|
@@ -11,6 +11,11 @@ import java.net.UnknownHostException;
|
|
*/
|
|
*/
|
|
|
|
|
|
public class IpUtils {
|
|
public class IpUtils {
|
|
|
|
+
|
|
|
|
+ private static final int INADDR4SZ = 4;
|
|
|
|
+ private static final int INADDR16SZ = 16;
|
|
|
|
+ private static final int INT16SZ = 2;
|
|
|
|
+
|
|
public static String getIpAddr(HttpServletRequest request) {
|
|
public static String getIpAddr(HttpServletRequest request) {
|
|
if (request == null) {
|
|
if (request == null) {
|
|
return "unknown";
|
|
return "unknown";
|
|
@@ -76,75 +81,55 @@ public class IpUtils {
|
|
/**
|
|
/**
|
|
* 将IPv4地址转换成字节
|
|
* 将IPv4地址转换成字节
|
|
*
|
|
*
|
|
- * @param text IPv4地址
|
|
|
|
|
|
+ * @param src IPv4地址
|
|
* @return byte 字节
|
|
* @return byte 字节
|
|
*/
|
|
*/
|
|
- public static byte[] textToNumericFormatV4(String text) {
|
|
|
|
- if (text.length() == 0) {
|
|
|
|
|
|
+ public static byte[] textToNumericFormatV4(String src)
|
|
|
|
+ {
|
|
|
|
+ byte[] res = new byte[INADDR4SZ];
|
|
|
|
+
|
|
|
|
+ long tmpValue = 0;
|
|
|
|
+ int currByte = 0;
|
|
|
|
+ boolean newOctet = true;
|
|
|
|
+
|
|
|
|
+ int len = src.length();
|
|
|
|
+ if (len == 0 || len > 15) {
|
|
return null;
|
|
return null;
|
|
}
|
|
}
|
|
|
|
|
|
- byte[] bytes = new byte[4];
|
|
|
|
- String[] elements = text.split("\\.", -1);
|
|
|
|
- try {
|
|
|
|
- long l;
|
|
|
|
- int i;
|
|
|
|
- switch (elements.length) {
|
|
|
|
- case 1:
|
|
|
|
- l = Long.parseLong(elements[0]);
|
|
|
|
- if ((l < 0L) || (l > 4294967295L)) {
|
|
|
|
- return null;
|
|
|
|
- }
|
|
|
|
- bytes[0] = (byte) (int) (l >> 24 & 0xFF);
|
|
|
|
- bytes[1] = (byte) (int) ((l & 0xFFFFFF) >> 16 & 0xFF);
|
|
|
|
- bytes[2] = (byte) (int) ((l & 0xFFFF) >> 8 & 0xFF);
|
|
|
|
- bytes[3] = (byte) (int) (l & 0xFF);
|
|
|
|
- break;
|
|
|
|
- case 2:
|
|
|
|
- l = Integer.parseInt(elements[0]);
|
|
|
|
- if ((l < 0L) || (l > 255L)) {
|
|
|
|
- return null;
|
|
|
|
- }
|
|
|
|
- bytes[0] = (byte) (int) (l & 0xFF);
|
|
|
|
- l = Integer.parseInt(elements[1]);
|
|
|
|
- if ((l < 0L) || (l > 16777215L)) {
|
|
|
|
- return null;
|
|
|
|
- }
|
|
|
|
- bytes[1] = (byte) (int) (l >> 16 & 0xFF);
|
|
|
|
- bytes[2] = (byte) (int) ((l & 0xFFFF) >> 8 & 0xFF);
|
|
|
|
- bytes[3] = (byte) (int) (l & 0xFF);
|
|
|
|
- break;
|
|
|
|
- case 3:
|
|
|
|
- for (i = 0; i < 2; ++i) {
|
|
|
|
- l = Integer.parseInt(elements[i]);
|
|
|
|
- if ((l < 0L) || (l > 255L)) {
|
|
|
|
- return null;
|
|
|
|
- }
|
|
|
|
- bytes[i] = (byte) (int) (l & 0xFF);
|
|
|
|
- }
|
|
|
|
- l = Integer.parseInt(elements[2]);
|
|
|
|
- if ((l < 0L) || (l > 65535L)) {
|
|
|
|
- return null;
|
|
|
|
- }
|
|
|
|
- bytes[2] = (byte) (int) (l >> 8 & 0xFF);
|
|
|
|
- bytes[3] = (byte) (int) (l & 0xFF);
|
|
|
|
- break;
|
|
|
|
- case 4:
|
|
|
|
- for (i = 0; i < 4; ++i) {
|
|
|
|
- l = Integer.parseInt(elements[i]);
|
|
|
|
- if ((l < 0L) || (l > 255L)) {
|
|
|
|
- return null;
|
|
|
|
- }
|
|
|
|
- bytes[i] = (byte) (int) (l & 0xFF);
|
|
|
|
- }
|
|
|
|
- break;
|
|
|
|
- default:
|
|
|
|
|
|
+ for (int i = 0; i < len; i++) {
|
|
|
|
+ char c = src.charAt(i);
|
|
|
|
+ if (c == '.') {
|
|
|
|
+ if (newOctet || tmpValue < 0 || tmpValue > 0xff || currByte == 3) {
|
|
|
|
+ return null;
|
|
|
|
+ }
|
|
|
|
+ res[currByte++] = (byte) (tmpValue & 0xff);
|
|
|
|
+ tmpValue = 0;
|
|
|
|
+ newOctet = true;
|
|
|
|
+ } else {
|
|
|
|
+ int digit = Character.digit(c, 10);
|
|
|
|
+ if (digit < 0) {
|
|
return null;
|
|
return null;
|
|
|
|
+ }
|
|
|
|
+ tmpValue *= 10;
|
|
|
|
+ tmpValue += digit;
|
|
|
|
+ newOctet = false;
|
|
}
|
|
}
|
|
- } catch (NumberFormatException e) {
|
|
|
|
|
|
+ }
|
|
|
|
+ if (newOctet || tmpValue < 0 || tmpValue >= (1L << ((4 - currByte) * 8))) {
|
|
return null;
|
|
return null;
|
|
}
|
|
}
|
|
- return bytes;
|
|
|
|
|
|
+ switch (currByte) {
|
|
|
|
+ case 0:
|
|
|
|
+ res[0] = (byte) ((tmpValue >> 24) & 0xff);
|
|
|
|
+ case 1:
|
|
|
|
+ res[1] = (byte) ((tmpValue >> 16) & 0xff);
|
|
|
|
+ case 2:
|
|
|
|
+ res[2] = (byte) ((tmpValue >> 8) & 0xff);
|
|
|
|
+ case 3:
|
|
|
|
+ res[3] = (byte) ((tmpValue >> 0) & 0xff);
|
|
|
|
+ }
|
|
|
|
+ return res;
|
|
}
|
|
}
|
|
|
|
|
|
public static String getHostIp() {
|
|
public static String getHostIp() {
|
|
@@ -162,4 +147,9 @@ public class IpUtils {
|
|
}
|
|
}
|
|
return "未知";
|
|
return "未知";
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+ public static boolean isIPv4LiteralAddress(String src) {
|
|
|
|
+ return textToNumericFormatV4(src) != null;
|
|
|
|
+ }
|
|
|
|
+
|
|
}
|
|
}
|