범위를 확인하는 가장 쉬운 방법은 32 비트 정수로 IP 주소를 변환 한 후 바로 정수를 비교하는 아마.
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()
, 당신은 DNS 조회의 가능성을 피하는
InetAddresses 헬퍼 클래스가있는 구아바 도서관에서 볼 수 있습니다.
감사합니다. 나는 이것을 시도 할 것입니다. –
이것은 훌륭했습니다. 감사! –
매우 도움이되었습니다. 감사합니다. – NitayArt