温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

基于Java检查IPv6地址的合法性

发布时间:2020-10-08 14:17:17 来源:脚本之家 阅读:155 作者:龙凌云端 栏目:编程语言

这篇文章主要介绍了基于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
----------------------------------------------------

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持亿速云。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI