Commit a6b4d3a6 authored by yanzg's avatar yanzg

消费者自动创建

parent f6fdb0b5
......@@ -2,8 +2,10 @@ package com.yanzuoguang.util.helper;
import com.yanzuoguang.util.exception.CodeException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.net.*;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
/**
* 地址字符串处理
......@@ -12,15 +14,15 @@ public class UrlHelper {
/**
* 转换为%E4%BD%A0形式
*
* @param s
* @param encoding
* @return
* @param from 来源字符串
* @param encoding 编码方式
* @return 转换后的结果
*/
public static String encoding(String s, String encoding) {
public static String encoding(String from, String encoding) {
try {
StringBuffer sb = new StringBuffer();
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
for (int i = 0; i < from.length(); i++) {
char c = from.charAt(i);
if (c >= 0 && c <= 255) {
sb.append(c);
} else {
......@@ -37,15 +39,60 @@ public class UrlHelper {
/**
* 将%E4%BD%A0转换为汉字
*
* @param s
* @param encoding
* @return
* @param from 来源字符串
* @param encoding 编码方式
* @return 转换后的结果
*/
public static String decoding(String s, String encoding) {
public static String decoding(String from, String encoding) {
try {
return URLDecoder.decode(s, encoding);
return URLDecoder.decode(from, encoding);
} catch (Exception ex) {
throw new CodeException(ex);
}
}
/**
* 获取IP
*
* @return 获取到的IP
*/
public static String getIp() {
List<String> ips = getIps();
if (ips.size() == 1) {
return ips.get(0);
}
for (String ip : ips) {
if (ip.equals("127.0.0.1") || ip.equals("0.0.0.0")) {
continue;
}
return ip;
}
return StringHelper.EMPTY;
}
/**
* 获取IP列表
*
* @return IP结果
*/
public static List<String> getIps() {
List<String> ipList = new ArrayList<String>();
try {
Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();
while (networkInterfaces.hasMoreElements()) {
NetworkInterface networkInterface = networkInterfaces.nextElement();
Enumeration<InetAddress> inetAddresses = networkInterface.getInetAddresses();
while (inetAddresses.hasMoreElements()) {
InetAddress inetAddress = inetAddresses.nextElement();
if (inetAddress != null && inetAddress instanceof Inet4Address) { // IPV4
String ip = inetAddress.getHostAddress();
ipList.add(ip);
}
}
}
} catch (SocketException e) {
e.printStackTrace();
}
return ipList;
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment