要在Java
中判斷一個IP地址
是否在給定的網段內,可以使用子網掩碼
將IP地址
和子網掩碼
進行與操作
來提取網絡地址,并將其與給定的子網地址進行比較。
方法一:借助于 Java 提供的 InetAddress
下面的例子由強大的 ChatGPT
提供。
代碼如下所示(子網掩碼的計算可以截取字符串后,借助底部的算法進行獲得):
public static boolean isIpAddressInSubnet(String ipAddress, String subnetAddress, String subnetMask) throws UnknownHostException {
InetAddress inetAddress = InetAddress.getByName(ipAddress);
InetAddress subnet = InetAddress.getByName(subnetAddress);
InetAddress mask = InetAddress.getByName(subnetMask);
byte[] inetAddressBytes = inetAddress.getAddress();
byte[] subnetBytes = subnet.getAddress();
byte[] maskBytes = mask.getAddress();
for (int i = 0; i < inetAddressBytes.length; i++) {
int addressByte = inetAddressBytes[i] & 0xFF;
int subnetByte = subnetBytes[i] & 0xFF;
int maskByte = maskBytes[i] & 0xFF;
if ((addressByte & maskByte) != (subnetByte & maskByte)) {
return false;
}
}
return true;
}
這個方法接受三個參數:要檢查的IP地址
、子網地址
和子網掩碼
。它使用InetAddress
類將這些字符串轉換為Java對象,然后將它們轉換為字節(jié)數組。然后,它迭代每個字節(jié),并將每個字節(jié)的值與相應的子網字節(jié)和掩碼字節(jié)進行比較,以提取網絡地址。如果網絡地址與子網地址不匹配,則返回false
,否則返回true
。
例如,假設我們要檢查IP地址 “192.168.1.100
” 是否在子網"192.168.1.0/24
"中:
boolean result = isIpAddressInSubnet("192.168.1.100", "192.168.1.0", "255.255.255.0");
if (result) {
System.out.println("IP address is in subnet");
} else {
System.out.println("IP address is not in subnet");
}
方法二:擼個算法實現(二進制計算)
代碼來源于 Google
,但是明白原理即可,不需要深究。不管怎么樣肯定都會需要 IP 地址
和子網掩碼
才能組成一個子網范圍;然后判斷另一個給定的 IP
是否在這個網段內。
/**
* 判斷是否在該網段中
*
* @param subnetRange 子網范圍 x.x.x.x/xx 形式
*/
public static boolean isIpAddressInSubnet(String ipAddress, String subnetRange) {
String[] networkips = ipAddress.split("\\.");
int ipAddr = (Integer.parseInt(networkips[0]) << 24)
| (Integer.parseInt(networkips[1]) << 16)
| (Integer.parseInt(networkips[2]) << 8)
| Integer.parseInt(networkips[3]);
// 拿到主機數
int type = Integer.parseInt(subnetRange.replaceAll(".*/", ""));
int ipCount = 0xFFFFFFFF << (32 - type);
String maskIp = subnetRange.replaceAll("/.*", "");
String[] maskIps = maskIp.split("\\.");
int cidrIpAddr = (Integer.parseInt(maskIps[0]) << 24)
| (Integer.parseInt(maskIps[1]) << 16)
| (Integer.parseInt(maskIps[2]) << 8)
| Integer.parseInt(maskIps[3]);
return (ipAddr & ipCount) == (cidrIpAddr & ipCount);
}
例如,假設我們要檢查IP地址 “192.168.1.100
” 是否在子網"192.168.1.0/24
"中:
boolean result = isIpAddressInSubnet("192.168.1.100", "192.168.1.0/24");
if (result) {
System.out.println("IP address is in subnet");
} else {
System.out.println("IP address is not in subnet");
}
其他
數字轉為子網掩碼
public static String subnetMaskFromPrefixLength(int prefixLength) {
if (prefixLength < 0 || prefixLength > 32) {
throw new IllegalArgumentException("Invalid prefix length");
}
int mask = 0xffffffff << (32 - prefixLength);
return String.format("%d.%d.%d.%d",
(mask & 0xff000000) >>> 24,
(mask & 0x00ff0000) >>> 16,
(mask & 0x0000ff00) >>> 8,
(mask & 0x000000ff));
}
示例:文章來源:http://www.zghlxwxcb.cn/news/detail-788084.html
String subnetMask = subnetMaskFromPrefixLength(24);
System.out.println(subnetMask); // 輸出:255.255.255.0
個人博客: Roc’s Blog文章來源地址http://www.zghlxwxcb.cn/news/detail-788084.html
到了這里,關于Java 快速判斷一個 IP 是否在給定的網段內的文章就介紹完了。如果您還想了解更多內容,請在右上角搜索TOY模板網以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網!