基于Java检查IPv6地址的合法性
程序员文章站
2023-08-24 18:59:29
这篇文章主要介绍了基于java检查ipv6地址的合法性,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
由于ipv4资源即将耗尽,i...
这篇文章主要介绍了基于java检查ipv6地址的合法性,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
由于ipv4资源即将耗尽,ipv6将要正式启用,这是大势所趋。
一些现有的服务和应用逐步要对ipv6支持,目前还处在过渡阶段。
提前了解一些ipv6的知识,还是有必要的。
如何判断一个ip地址,是否为ipv6地址呢?
以下代码是用java实现的,用于校验ipv6地址合法性。
代码如下:
import java.text.normalizer; import java.text.normalizer.form; import java.util.regex.matcher; import java.util.regex.pattern; public class ipv6check { public static void main(string[] args) { string ipv6_1 = "2019:db8:a583:64:c68c:d6df:600c:ee9a"; string ipv6_2 = "2019:db8:a583::9e42:be55:53a7"; string ipv6_3 = "2019:db8:a583:::9e42:be55:53a7"; string ipv6_4 = "1:2:3:4:5::192.168.254.254"; string ipv6_5 = "abcd:910a:2222:5498:8475:1111:3900:2020"; string ipv6_6 = "1030::c9b4:ff12:48aa:1a2b"; string ipv6_7 = "2019:0:0:0:0:0:0:1"; string ipv6_8 = "::0:0:0:0:0:0:1"; string ipv6_9 = "2019:0:0:0:0::"; string ipv6_10= "2048:877e:31::7"; string resultline = "\n==> "; string splitline = "\n----------------------------------------------------\n"; system.out.println(ipv6_1 + resultline + isvalidipv6addr(ipv6_1) + splitline); system.out.println(ipv6_2 + resultline + isvalidipv6addr(ipv6_2) + splitline); system.out.println(ipv6_3 + resultline + isvalidipv6addr(ipv6_3) + splitline); system.out.println(ipv6_4 + resultline + isvalidipv6addr(ipv6_4) + splitline); system.out.println(ipv6_5 + resultline + isvalidipv6addr(ipv6_5) + splitline); system.out.println(ipv6_6 + resultline + isvalidipv6addr(ipv6_6) + splitline); system.out.println(ipv6_7 + resultline + isvalidipv6addr(ipv6_7) + splitline); system.out.println(ipv6_8 + resultline + isvalidipv6addr(ipv6_8) + splitline); system.out.println(ipv6_9 + resultline + isvalidipv6addr(ipv6_9) + splitline); system.out.println(ipv6_10 + resultline + isvalidipv6addr(ipv6_10) + splitline); } /** * 校验ipv6地址的合法性 * @param ipaddr * @return */ public static boolean isvalidipv6addr(string ipaddr) { string regex = "(^((([0-9a-fa-f]{1,4}:){7}(([0-9a-fa-f]{1,4}){1}|:))" + "|(([0-9a-fa-f]{1,4}:){6}((:[0-9a-fa-f]{1,4}){1}|" + "((22[0-3]|2[0-1][0-9]|[0-1][0-9][0-9]|" + "([0-9]){1,2})([.](25[0-5]|2[0-4][0-9]|" + "[0-1][0-9][0-9]|([0-9]){1,2})){3})|:))|" + "(([0-9a-fa-f]{1,4}:){5}((:[0-9a-fa-f]{1,4}){1,2}|" + ":((22[0-3]|2[0-1][0-9]|[0-1][0-9][0-9]|" + "([0-9]){1,2})([.](25[0-5]|2[0-4][0-9]|" + "[0-1][0-9][0-9]|([0-9]){1,2})){3})|:))|" + "(([0-9a-fa-f]{1,4}:){4}((:[0-9a-fa-f]{1,4}){1,3}" + "|:((22[0-3]|2[0-1][0-9]|[0-1][0-9][0-9]|" + "([0-9]){1,2})([.](25[0-5]|2[0-4][0-9]|[0-1][0-9][0-9]|" + "([0-9]){1,2})){3})|:))|(([0-9a-fa-f]{1,4}:){3}((:[0-9a-fa-f]{1,4}){1,4}|" + ":((22[0-3]|2[0-1][0-9]|[0-1][0-9][0-9]|" + "([0-9]){1,2})([.](25[0-5]|2[0-4][0-9]|" + "[0-1][0-9][0-9]|([0-9]){1,2})){3})|:))|" + "(([0-9a-fa-f]{1,4}:){2}((:[0-9a-fa-f]{1,4}){1,5}|" + ":((22[0-3]|2[0-1][0-9]|[0-1][0-9][0-9]|" + "([0-9]){1,2})([.](25[0-5]|2[0-4][0-9]|" + "[0-1][0-9][0-9]|([0-9]){1,2})){3})|:))" + "|(([0-9a-fa-f]{1,4}:){1}((:[0-9a-fa-f]{1,4}){1,6}" + "|:((22[0-3]|2[0-1][0-9]|[0-1][0-9][0-9]|" + "([0-9]){1,2})([.](25[0-5]|2[0-4][0-9]|" + "[0-1][0-9][0-9]|([0-9]){1,2})){3})|:))|" + "(:((:[0-9a-fa-f]{1,4}){1,7}|(:[ff]{4}){0,1}:((22[0-3]|2[0-1][0-9]|" + "[0-1][0-9][0-9]|([0-9]){1,2})" + "([.](25[0-5]|2[0-4][0-9]|[0-1][0-9][0-9]|([0-9]){1,2})){3})|:)))$)"; if (ipaddr == null) { system.out.println("ipv6 address is null "); return false; } ipaddr = normalizer.normalize(ipaddr, form.nfkc); pattern pattern = pattern.compile(regex); matcher matcher = pattern.matcher(ipaddr); boolean match = matcher.matches(); if (!match) { system.out.println("invalid ipv6 address = " + ipaddr); } return match; } }
运行结果如下:
2019:db8:a583:64:c68c:d6df:600c:ee9a ==> true ---------------------------------------------------- 2019:db8:a583::9e42:be55:53a7 ==> true ---------------------------------------------------- invalid ipv6 address = 2019:db8:a583:::9e42:be55:53a7 2019:db8:a583:::9e42:be55:53a7 ==> false ---------------------------------------------------- 1:2:3:4:5::192.168.254.254 ==> true ---------------------------------------------------- abcd:910a:2222:5498:8475:1111:3900:2020 ==> true ---------------------------------------------------- 1030::c9b4:ff12:48aa:1a2b ==> true ---------------------------------------------------- 2019:0:0:0:0:0:0:1 ==> true ---------------------------------------------------- ::0:0:0:0:0:0:1 ==> true ---------------------------------------------------- 2019:0:0:0:0:: ==> true ---------------------------------------------------- 2048:877e:31::7 ==> true ----------------------------------------------------
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。