Java 检查ipAddress是否在私有范围内
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9729378/
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 whether the ipAddress is in private range
提问by AKIWEB
How would I check to see whether the ip address is in private category ?
我将如何检查 IP 地址是否属于私有类别?
if(isPrivateIPAddress(ipAddress)) {
//do something
}
Any suggestions will be appreciated.
任何建议将不胜感激。
UPDATED ANSWER
更新的答案
private static boolean isPrivateIPAddress(String ipAddress) {
InetAddress ia = null;
try {
InetAddress ad = InetAddress.getByName(ipAddress);
byte[] ip = ad.getAddress();
ia = InetAddress.getByAddress(ip);
} catch (UnknownHostException e) {
e.printStackTrace();
}
return ia.isSiteLocalAddress();
}
I wrote this method and it's working fine for me. But is there any case in which this method will not work ? I just wanted to make sure it will be working for every case.
我写了这个方法,它对我来说很好用。但是有没有这种方法不起作用的情况?我只是想确保它适用于每种情况。
采纳答案by BillThor
This is a quick hack I generated to test my own address.
这是我生成的一个快速黑客来测试我自己的地址。
import java.net.InetAddress;
import java.net.UnknownHostException;
public class LocalAddress {
public static void main(String[] args) {
InetAddress address = null;
try {
address = InetAddress.getLocalHost();
} catch (UnknownHostException e) {
e.printStackTrace();
}
if (address.isSiteLocalAddress()) {
System.out.println("Site Local Address: " + address.getHostAddress());
} else {
System.out.println("Routeable Address: " + address.getHostAddress());
}
}
}
EDIT: This code has not been tested for the link local addresses, localhost, or address blocks reserved for documentation. The first two cases have methods that return them. The last is not referenced in the documentation of the class.
编辑:此代码尚未针对链接本地地址、本地主机或为文档保留的地址块进行测试。前两种情况具有返回它们的方法。最后一个在类的文档中没有被引用。
回答by Nick
The correct method is InetAddress.isSiteLocalAddress().
正确的方法是InetAddress.isSiteLocalAddress()。
Utility routine to check if the InetAddress is a site local address.
Returns: a boolean indicating if the InetAddress is a site local address; or false if address is not a site local unicast address.
检查 InetAddress 是否为站点本地地址的实用程序例程。
返回: 指示 InetAddress 是否为站点本地地址的布尔值;如果地址不是站点本地单播地址,则为 false。
回答by sofia
First of all, Private networks can use IPv4 addresses anywhere in the following ranges:
首先,专用网络可以在以下范围内的任何地方使用 IPv4 地址:
- a) 192.168.0.0 - 192.168.255.255 (65,536 IP addresses)
- b) 172.16.0.0 - 172.31.255.255 (1,048,576 IP addresses)
- c) 10.0.0.0 - 10.255.255.255 (16,777,216 IP addresses)
- a) 192.168.0.0 - 192.168.255.255(65,536 个 IP 地址)
- b) 172.16.0.0 - 172.31.255.255(1,048,576 个 IP 地址)
- c) 10.0.0.0 - 10.255.255.255(16,777,216 个 IP 地址)
As we can see from method isSiteLocalAddress in Inet4Address.java :
正如我们从 Inet4Address.java 中的 isSiteLocalAddress 方法中看到的:
public boolean isSiteLocalAddress() {
// refer to RFC 1918
// 10/8 prefix
// 172.16/12 prefix
// 192.168/16 prefix
int address = holder().getAddress();
return (((address >>> 24) & 0xFF) == 10)
|| ((((address >>> 24) & 0xFF) == 172)
&& (((address >>> 16) & 0xF0) == 16))
|| ((((address >>> 24) & 0xFF) == 192)
&& (((address >>> 16) & 0xFF) == 168));
}
So case b) 172.16.0.0 - 172.31.255.255 (1,048,576 IP addresses) is not satisfied. But you can easily write you own version of how to tell whether a address is private address.Here is my version:
因此不满足情况 b) 172.16.0.0 - 172.31.255.255(1,048,576 个 IP 地址)。但是你可以很容易地写出你自己的版本如何判断一个地址是否是私人地址。这是我的版本:
import com.google.common.net.InetAddresses;
private static boolean isPrivateV4Address(String ip) {
int address = InetAddresses.coerceToInteger(InetAddresses.forString(ip));
return (((address >>> 24) & 0xFF) == 10)
|| ((((address >>> 24) & 0xFF) == 172)
&& ((address >>> 16) & 0xFF) >= 16
&& ((address >>> 16) & 0xFF) <= 31)
|| ((((address >>> 24) & 0xFF) == 192)
&& (((address >>> 16) & 0xFF) == 168));
}
回答by JorSol
I use this:
我用这个:
public boolean isPrivateIP(String ipAddress) {
boolean isValid = false;
if (ipAddress != null && !ipAddress.isEmpty()) {
String[] ip = ipAddress.split("\.");
short[] ipNumber = new short[] {
Short.parseShort(ip[0]),
Short.parseShort(ip[1]),
Short.parseShort(ip[2]),
Short.parseShort(ip[3])
};
if (ipNumber[0] == 10) { // Class A
isValid = true;
} else if (ipNumber[0] == 172 && (ipNumber[1] >= 16 && ipNumber[1] <= 31)) { // Class B
isValid = true;
} else if (ipNumber[0] == 192 && ipNumber[1] == 168) { // Class C
isValid = true;
}
}
return isValid;
}