我如何检查IP地址是否属于私有类别?
if(isPrivateIPAddress(ipAddress)) {
//do something
}
Run Code Online (Sandbox Code Playgroud)
任何建议将不胜感激.
更新的答案
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();
}
Run Code Online (Sandbox Code Playgroud)
我写了这个方法,它对我来说很好.但有没有这种方法不起作用的情况?我只是想确保它适用于每个案例.
java ×1