在 Java 中检查字符串是主机名还是 IP 地址
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9954140/
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
Check if a string is a hostname or an ip-address in Java
提问by Horcrux7
I want to check if a string is a hostname or an ip-address in Java. Is there an API to do it or must I write a parser myself?
我想检查字符串是 Java 中的主机名还是 IP 地址。是否有 API 可以做到这一点,还是我必须自己编写解析器?
The problem is complex because there are IPv4 addresses, short and long IPv6 addresses, short hostnames and FQDN host names.
这个问题很复杂,因为有 IPv4 地址、短和长 IPv6 地址、短主机名和 FQDN 主机名。
采纳答案by MrGomez
While you could theoretically write rules to handle these cases yourself, using theusualcadreof RFCs, I'd instead look at the entirety of this class in Google Guava, especially the corner cases, such as how it resolves the embedding of 4-in-6 addresses.
虽然理论上你就可以编写规则来自己处理这些情况下,使用了通常干部的RFC文档,我不是看这个类在谷歌番石榴的整体,尤其是极端案例,比如它是如何解决的4-在嵌入-6 个地址。
As for determining if you have a FQDN, see if coercion to IP address fails, then try to resolve it against DNS. Anything else should, given your input cases, be a hostname or local resolution that isn't fully qualified.
至于确定你是否有FQDN,看看强制IP地址是否失败,然后尝试针对DNS进行解析。考虑到您的输入情况,其他任何内容都应该是不完全限定的主机名或本地解析。
回答by BluesRockAddict
There doesn't appear to be an API, but writing such function doesn't seem to be very difficult. You can check the following conditions in your code:
貌似没有API,但是写这样的函数好像不是很困难。您可以在代码中检查以下条件:
- If sourceStr contains ":" but no "." -> IPv6
- If sourceStr.matches("^.[0-9]{1,3}/..[0-9]{1,3}/..[0-9]{1,3}/..[0-9]{1,3}") == true) -> IPv4
- If sourceStr contains "." -> FQDN host name
- Otherwise it must be a short hostname
- 如果 sourceStr 包含 ":" 但没有 "." -> IPv6
- 如果 sourceStr.matches("^.[0-9]{1,3}/..[0-9]{1,3}/..[0-9]{1,3}/..[0- 9]{1,3}") == true) -> IPv4
- 如果 sourceStr 包含“.” -> FQDN 主机名
- 否则它必须是一个短主机名
回答by Chikei
Not possible in JDK API but you can use InetAddresses.forString
in google guavawhich throws exception when argument is not a IP string literal.
在 JDK API 中不可能,但您可以InetAddresses.forString
在google guava中使用,当参数不是 IP 字符串文字时会引发异常。
回答by Sean F
The IPAddress Java librarywill do it. The javadoc is available at the link. Disclaimer: I am the project manager.
IPAddress Java 库会做到这一点。链接中提供了 javadoc。免责声明:我是项目经理。
static void check(HostName host) {
try {
host.validate();
if(host.isAddress()) {
System.out.println("address: " + host.asAddress());
} else {
System.out.println("host name: " + host);
}
} catch(HostNameException e) {
System.out.println(e.getMessage());
}
}
public static void main(String[] args) {
HostName host = new HostName("1.2.3.4");
check(host);
host = new HostName("1.2.a.4");
check(host);
host = new HostName("::1");
check(host);
host = new HostName("[::1]");
check(host);
host = new HostName("1.2.?.4");
check(host);
}
Output:
输出:
address: 1.2.3.4
host name: 1.2.a.4
address: ::1
address: ::1
1.2.?.4 Host error: invalid character at index 4
回答by GMouaad
To expand @MrGomez 's answer, Use InetAddressesto validate IP Addresses and InternetDomainNameto validate hostnames (FQDN).
要扩展@MrGomez 的答案,请使用InetAddresses验证 IP 地址和InternetDomainName验证主机名 (FQDN)。
Example:
例子:
public static boolean validate(final String hostname) {
return InetAddresses.isUriInetAddress(hostname) || InternetDomainName.isValid(hostname);
}