<返回更多

java 判断IP地址是否合法,是IPV4,还是IPV6

2019-09-18    
加入收藏

/**

* 判断所有的IP地址

* @param IP

* @return

*/

public String validIPAddressAll(String IP) {

if (!IP.contains(".") && !IP.contains(":")) {

return "Neither";

}

//如果是IPV4

if (IP.contains(".")) {

if (IP.endsWith(".")) {

return "Neither";

}

String[] arr = IP.split("\.");

if (arr.length != 4) {

return "Neither";

}

for (int i = 0; i < 4; i++) {

if (arr[i].length() == 0 || arr[i].length() > 3) {

return "Neither";

}

for (int j = 0; j < arr[i].length(); j++) {

if (arr[i].charAt(j) >= '0' && arr[i].charAt(j) <= '9') {

continue;

}

return "Neither";

}

if (Integer.valueOf(arr[i]) > 255 || (arr[i].length() >= 2 && String.valueOf(arr[i]).startsWith("0"))) {

return "Neither";

}

}

return "IPv4";

}//如果是IPV4

//如果是IPV6

if (IP.contains(":")) {

if (IP.endsWith(":") && !IP.endsWith("::")) {

return "Neither";

}

//如果包含多个“::”,一个IPv6地址中只能出现一个“::”

if (IP.indexOf("::") != -1 && IP.indexOf("::", IP.indexOf("::") + 2) != -1) {

return "Neither";

}

//如果含有一个“::”

if (IP.contains("::")) {

String[] arr = IP.split(":");

if (arr.length > 7 || arr.length < 1) {//"1::"是最短的字符串

return "Neither";

}

for (int i = 0; i < arr.length; i++) {

if (arr[i].equals("")) {

continue;

}

if (arr[i].length() > 4) {

return "Neither";

}

for (int j = 0; j < arr[i].length(); j++) {

if ((arr[i].charAt(j) >= '0' && arr[i].charAt(j) <= '9') || (arr[i].charAt(j) >= 'A' && arr[i].charAt(j) <= 'F')

|| (arr[i].charAt(j) >= 'a' && arr[i].charAt(j) <= 'f')) {

continue;

}

return "Neither";

}

}

return "IPv6";

}

//如果不含有“::”

if (!IP.contains("::")) {

String[] arr = IP.split(":");

if (arr.length != 8) {

return "Neither";

}

for (int i = 0; i < arr.length; i++) {

if (arr[i].length() > 4) {

return "Neither";

}

for (int j = 0; j < arr[i].length(); j++) {

if ((arr[i].charAt(j) >= '0' && arr[i].charAt(j) <= '9') || (arr[i].charAt(j) >= 'A' && arr[i].charAt(j) <= 'F')

|| (arr[i].charAt(j) >= 'a' && arr[i].charAt(j) <= 'f')) {

continue;

}

return "Neither";

}

}

return "IPv6";

}

}//如果是IPV6

return "Neither";

}

System.out.println(object.validIPAddressAll("172.16.254.1"));//IPv4

System.out.println(object.validIPAddressAll("1e1.4.5.6"));//Neither

System.out.println(object.validIPAddressAll("12..33.4"));//Neither

System.out.println(object.validIPAddressAll("1212.132123.33434.41221"));//Neither

System.out.println(object.validIPAddressAll("2001:0db8:85a3:0:0:8A2E:0370:7334:"));//Neither

System.out.println(object.validIPAddressAll("2001:0db8:85a3:000:0:8A2E:0370:7334"));//IPv6

System.out.println(object.validIPAddressAll("2001:0db8:85a3:000:0:8A2E:0370:73d34"));//Neither

System.out.println(object.validIPAddressAll("::"));//Neither

System.out.println(object.validIPAddressAll("::1"));//IPv6

System.out.println(object.validIPAddressAll("::1"));//Neither

System.out.println(object.validIPAddressAll("123::566::ae4"));//Neither

System.out.println(object.validIPAddressAll("2001:0db8:85a3:000:0:8A2E::"));//IPv6

System.out.println(object.validIPAddressAll("2001:0db8:85a3:vcsd:0:8A2E::"));//Neither

以上基于jdk1.8

————————————————

版权声明:本文为CSDN博主「二十六画生的博客」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。

原文链接:https://blog.csdn.net/u010002184/article/details/93179294

声明:本站部分内容来自互联网,如有版权侵犯或其他问题请与我们联系,我们将立即删除或处理。
▍相关推荐
更多资讯 >>>