检查范围的最简单方法可能是将IP地址转换为32位整数,然后比较这些整数。
public class Example { public static long ipToLong(InetAddress ip) { byte[] octets = ip.getAddress(); long result = 0; for (byte octet : octets) { result <<= 8; result |= octet & 0xff; } return result; } public static void main(String[] args) throws UnknownHostException { long ipLo = ipToLong(InetAddress.getByName("192.200.0.0")); long ipHi = ipToLong(InetAddress.getByName("192.255.0.0")); long ipToTest = ipToLong(InetAddress.getByName("192.200.3.0")); System.out.println(ipToTest >= ipLo && ipToTest <= ipHi); }}而不是
InetAddress.getByName(),您可能想要查看具有InetAddresses帮助器类的Guava库,该类避免了DNS查找的可能性。



