Java - 如何检测 IP 版本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18102672/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 22:24:13  来源:igfitidea点击:

Java - How to detect IP version

javaservletsnetwork-programming

提问by tokhi

I'm getting ClientIP address via below method :

Client通过以下方法获取IP 地址:

public static String getClientIpAddr(HttpServletRequest request) {  
    String ip = request.getHeader("X-Forwarded-For");  
    ...
    return ip
} 

Now I want to detect if it is an IPV4or an IPV6.

现在我想检测它是 anIPV4还是IPV6.

采纳答案by Bex

You could create an InetAddress and check if it became an ipv4 or ipv6 instance

您可以创建一个 InetAddress 并检查它是否成为 ipv4 或 ipv6 实例

InetAddress address = InetAddress.getByName(ip);
if (address instanceof Inet6Address) {
    // It's ipv6
} else if (address instanceof Inet4Address) {
    // It's ipv4
}

It seems a bit awkward, though, and I hope there is a better solution.

不过,这似乎有点尴尬,我希望有更好的解决方案。

回答by morgano

If you are sure you're getting either an IPv4 or IPv6, you can try the following. If you have a DNS name then this will try to perform a lookup. Anyway, try this:

如果您确定自己使用的是 IPv4 或 IPv6,则可以尝试以下操作。如果您有 DNS 名称,那么这将尝试执行查找。无论如何,试试这个:

try {

    InetAddress address = InetAddress.getByName(myIpAddr);

    if (address instanceof Inet4Address) {
        // your IP is IPv4
    } else if (address instanceof Inet6Address) {
        // your IP is IPv6
    }

} catch(UnknownHostException e) {

    //  your address was a machine name like a DNS name, and couldn't be found

}

回答by tokhi

Thanks to Tala.

感谢塔拉

This is what I have tried using of this examplewith minor changes:

这是我尝试使用此示例并稍作更改的内容:

  private static Pattern VALID_IPV4_PATTERN = null;
  private static Pattern VALID_IPV6_PATTERN = null;
  private static final String ipv4Pattern = "(([01]?\d\d?|2[0-4]\d|25[0-5])\.){3}([01]?\d\d?|2[0-4]\d|25[0-5])";
  private static final String ipv6Pattern = "([0-9a-f]{1,4}:){7}([0-9a-f]){1,4}";

  static {
    try {
      VALID_IPV4_PATTERN = Pattern.compile(ipv4Pattern, Pattern.CASE_INSENSITIVE);
      VALID_IPV6_PATTERN = Pattern.compile(ipv6Pattern, Pattern.CASE_INSENSITIVE);
    } catch (PatternSyntaxException e) {
      //logger.severe("Unable to compile pattern", e);
    }
  }

  public static String isIpAddressV4orV6(String ipAddress) {

        Matcher ipv4 = IP_Utilities.VALID_IPV4_PATTERN.matcher(ipAddress);
        if (ipv4.matches()) {
          return "IPV4";
        }
        Matcher ipv6 = IP_Utilities.VALID_IPV6_PATTERN.matcher(ipAddress);
        if (ipv6.matches()) {
              return "IPV6";
        }
        return "";
    }

回答by Vladislav Kysliy

You can use InetAddresses from google guava. For example like this:

您可以使用谷歌番石榴中的 InetAddresses。例如像这样:

int addressLength = InetAddresses.forString(ip).getAddress().length;
switch (addressLength) {
    case 4:
        System.out.println("IPv4");
        break;
    case 16:
        System.out.println("IPv6");
        break;
    default:
        throw new IllegalArgumentException("Incorrect ip address length " + addressLength);
}