在 Java 中检查有效的 IPv4 地址

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7550806/
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-10-30 20:27:11  来源:igfitidea点击:

Check valid IPv4 Address in Java

javastringipv6ipv4

提问by AngelsandDemons

I am using the sun.net.util.IPAddressUtilpackage to check whether the string contains a valid IPv4 and IPv6 address or not.

我正在使用该sun.net.util.IPAddressUtil包来检查字符串是否包含有效的 IPv4 和 IPv6 地址。

Code Snippet is:-

代码片段是:-

String ipv4addr="200";

    if(IPAddressUtil.isIPv4LiteralAddress(ipv4addr))
    {
        System.out.println("valid ipv4 address");
    }
    else
    {
        System.out.println("not valid");

    }

But for addresses such as 200 and 300 it is still saying it is a valid IPv4 address, which it isn't. When I used the same package and checked for IPV6 address using :-

但是对于诸如 200 和 300 之类的地址,它仍然说它是有效的 IPv4 地址,而事实并非如此。当我使用相同的包并使用以下方法检查 IPV6 地址时:-

String ipv6addr="200";

    if(IPAddressUtil.isIPv6LiteralAddress(ipv6addr))
    {
        System.out.println("valid ipv6 address");
    }
    else
    {
        System.out.println("not valid");

    }

I get the correct result. However, IPv4 does not seem to be working or may be I am using it incorrectly. Please guide me. I don't want to use regex for IPv4 validation...

我得到正确的结果。但是,IPv4 似乎不起作用,或者可能是我使用不当。请指导我。我不想使用正则表达式进行 IPv4 验证...

回答by cHao

There's a reason you're getting a "valid" result: 200is a valid IPv4 address.

您得到“有效”结果是有原因的: 200是有效的 IPv4 地址。

See, to the computer, an IPv4 address is just a 32-bit number. The dots are entirely for our convenience, because we humans suck at memorizing big precise numbers. But they don't have to be there; there are rules about how an address gets parsed depending on how many parts it has.

看,对于计算机来说,IPv4 地址只是一个 32 位的数字。这些点完全是为了我们的方便,因为我们人类很难记住大的精确数字。但他们不必在那里;关于如何解析地址的规则取决于它有多少部分。

When an address consists of one number, it's considered a 32-bit number, and each byte is 8 bits of that number. If you were to parse "200"as an IP address, it would be equivalent to 0.0.0.200. Likewise, "2130706433"would be equivalent to 127.0.0.1.

当一个地址由一个数字组成时,它被认为是一个 32 位数字,每个字节是该数字的 8 位。如果解析"200"为 IP 地址,则相当于 0.0.0.200。同样,"2130706433"将等效于 127.0.0.1。

There are also standards for when an address has two parts like 0.200 (first part is the first byte, and the second part is a 24-bit number representing the other 3 bytes), and even 0.0.200 (first two numbers are bytes, the last part is 16 bits and takes up the other 2 bytes). The "unusual" formats are leftovers from the days of IP address classes, but almost all software that has to parse addresses will understand them. (If you pop open your browser and go to http://1249739112*or even http://74.125.33128*, for example, Google's home page will come up.)

当地址有两个部分时也有标准,例如 0.200(第一部分是第一个字节,第二部分是代表其他 3 个字节的 24 位数字),甚至 0.0.200(前两个数字是字节,最后一部分是 16 位并占用其他 2 个字节)。“不寻常”的格式是 IP 地址类时代遗留下来的,但几乎所有必须解析地址的软件都会理解它们。(例如,如果您打开浏览器并转到http://1249739112*甚至http://74.125.33128*,则会出现 Google 的主页。)

* See the comments for clickable links. Thanks, "link validator". :P

* 请参阅可点击链接的评论。谢谢,“链接验证器”。:P

See http://download.oracle.com/javase/6/docs/api/java/net/Inet4Address.htmlor http://www.perlmonks.org/?node_id=221512, or http://en.wikipedia.org/wiki/IPv4#Address_representations, for some more details.

请参阅http://download.oracle.com/javase/6/docs/api/java/net/Inet4Address.htmlhttp://www.perlmonks.org/?node_id=221512http://en.wikipedia .org/wiki/IPv4#Address_representations,了解更多详情。

Java understands these formats as well (as does .net, as well as any decent OS), and parses the address correctly whether it contains 1, 2, 3, or 4 parts.

Java 也理解这些格式(就像 .net 以及任何像样的操作系统一样),并且无论地址包含 1、2、3 还是 4 部分,都可以正确解析地址。

If you want to check that a would-be address actually looks like "xxx.xxx.xxx.xxx", then you'll probably want to explicitly check that using a pattern, or using a validation library that considers 32-bit numbers as invalid addresses (even though they are valid). I wouldn't bother, though -- if you use the lookup functions provided, you can accept an address in any standard format and it will work.

如果您想检查一个可能的地址实际上看起来像“xxx.xxx.xxx.xxx”,那么您可能需要使用模式或使用将 32 位数字视为无效地址(即使它们是有效的)。不过,我不会打扰——如果您使用提供的查找功能,您可以接受任何标准格式的地址并且它会起作用。

(All this mess changes with IPv6; there's a much stricter format, and you can't just type in some 36-digit number and expect it to work. But the platform still knows how to parse an address, and you should trust it to do so.)

(所有这些混乱都随着 IPv6 而改变;有更严格的格式,你不能只输入一些 36 位数字并期望它工作。但平台仍然知道如何解析地址,你应该相信它这样做。)

回答by Kohányi Róbert

Check out Guava'sInetAddressesclass which contains static utility methods for working with IP addresses. (As I understand it uses the sun.net.util.IPAddressUtilclass behind the scenes.)

查看Guava 的InetAddresses类,其中包含用于处理 IP 地址的静态实用程序方法。(据我所知,它sun.net.util.IPAddressUtil在幕后使用了该类。)

System.out.println(InetAddresses.isInetAddress("400")); // false

回答by MeBigFatGuy

It's not a good idea to use internal "sun" packaged classes, I'd try using Apache's Validator

使用内部“sun”打包类不是一个好主意,我会尝试使用 Apache 的 Validator

http://commons.apache.org/validator/

http://commons.apache.org/validator/

which has IP Address validation.

它具有 IP 地址验证。

回答by grebulon

If you want to validate if a string is valid IP address representation, the source code of org.apache.http.conn.util.InetAddressUtilsuses these regular expressions:

如果你想验证一个字符串是否是有效的 IP 地址表示,org.apache.http.conn.util.InetAddressUtils的源代码使用这些正则表达式:

IPV4_PATTERN = Pattern.compile(
    "^(25[0-5]|2[0-4]\d|[0-1]?\d?\d)(\.(25[0-5]|2[0-4]\d|[0-1]?\d?\d)){3}$");
IPV6_STD_PATTERN = Pattern.compile(
    "^(?:[0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}$");
IPV6_HEX_COMPRESSED_PATTERN = Pattern.compile(
    "^((?:[0-9A-Fa-f]{1,4}(?::[0-9A-Fa-f]{1,4})*)?)::((?:[0-9A-Fa-f]{1,4}(?::[0-9A-Fa-f]{1,4})*)?)$");

回答by Sean F

That string is an IPv4 string format that was originally introduced by the aton_inet utility in BSD Unix and has persisted until this day in the various Unix and Linux flavours and elsewhere.

该字符串是一种 IPv4 字符串格式,最初由 BSD Unix 中的 aton_inet 实用程序引入,并且一直持续到今天,在各种 Unix 和 Linux 风格以及其他地方。

https://linux.die.net/man/3/inet_aton

https://linux.die.net/man/3/inet_aton

The IPAddress Java librarywill do validation that can be configured to support aton_inet formats or not. The javadoc is available at the link. Disclaimer: I am the project manager.

IPAddress Java 库将执行可以配置为是否支持 aton_inet 格式的验证。链接中提供了 javadoc。免责声明:我是项目经理。

Verify if an address is valid, allow inet_aton style:

验证地址是否有效,允许 inet_aton 样式:

    String str = "200";
    IPAddressString addrString = new IPAddressString(str);
    try {
         IPAddress addr = addrString.toAddress();
         System.out.println("valid address: " + addr.toCanonicalString());
    } catch(IPAddressStringException e) {
        System.out.println(e.getMessage());
    }

Output:

输出:

valid address: 0.0.0.200

有效地址:0.0.0.200

Verify if an address is valid, do not allow inet_aton style:

验证地址是否有效,不允许 inet_aton 样式:

    IPAddressStringParameters parameters = new       
      IPAddressStringParameters.Builder().allow_inet_aton(false).toParams();
    addrString = new IPAddressString(str, parameters);
    try {
         IPAddress addr = addrString.toAddress();
         System.out.println("valid address: " + addr.toCanonicalString());
    } catch(IPAddressStringException e) {
        System.out.println(e.getMessage());
    }

Output:

输出:

200 IP Address error: options do not allow IPv4 address with less than four segments

200 IP 地址错误:选项不允许少于四段的 IPv4 地址

回答by Johntor

After a small research I ended up with something like this

经过一番小小的研究,我最终得到了这样的东西

    public static boolean isValidIP4Address(String ipAddress) {
        if (ipAddress.matches("^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$")) {
            String[] groups = ipAddress.split("\.");

            for (int i = 0; i <= 3; i++) {
                String segment = groups[i];
                if (segment == null || segment.length() <= 0) {
                    return false;
                }

                int value = 0;
                try {
                    value = Integer.parseInt(segment);
                } catch (NumberFormatException e) {
                    return false;
                }
                if (value > 255) {
                    return false;
                }
            }
            return true;
        }
        return false;
    }

which was fine for simple checks.

这对于简单的检查来说很好。