Java如何判斷一個IP是否在給定的網(wǎng)段內(nèi)
要在Java中判斷一個IP地址是否在給定的網(wǎng)段內(nèi),可以使用子網(wǎng)掩碼將IP地址和子網(wǎng)掩碼進行與操作來提取網(wǎng)絡(luò)地址,并將其與給定的子網(wǎng)地址進行比較。
方法一:借助于 Java 提供的 InetAddress
下面的例子由強大的 ChatGPT 提供。

代碼如下所示(子網(wǎng)掩碼的計算可以截取字符串后,借助底部的算法進行獲得):
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;
}這個方法接受三個參數(shù):
要檢查的IP地址、子網(wǎng)地址和子網(wǎng)掩碼。它使用InetAddress類將這些字符串轉(zhuǎn)換為Java對象,然后將它們轉(zhuǎn)換為字節(jié)數(shù)組。然后,它迭代每個字節(jié),并將每個字節(jié)的值與相應(yīng)的子網(wǎng)字節(jié)和掩碼字節(jié)進行比較,以提取網(wǎng)絡(luò)地址。
如果網(wǎng)絡(luò)地址與子網(wǎng)地址不匹配,則返回false,否則返回true。
例如,假設(shè)我們要檢查IP地址 “192.168.1.100” 是否在子網(wǎng)"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");
}方法二:擼個算法實現(xiàn)(二進制計算)
代碼來源于 Google,但是明白原理即可,不需要深究。
不管怎么樣肯定都會需要 IP 地址和子網(wǎng)掩碼才能組成一個子網(wǎng)范圍;然后判斷另一個給定的 IP 是否在這個網(wǎng)段內(nèi)。
/**
* 判斷是否在該網(wǎng)段中
*
* @param subnetRange 子網(wǎng)范圍 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]);
// 拿到主機數(shù)
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);
}例如,假設(shè)我們要檢查IP地址 “192.168.1.100” 是否在子網(wǎng)"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");
}其他
數(shù)字轉(zhuǎn)為子網(wǎng)掩碼
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));
}示例:
String subnetMask = subnetMaskFromPrefixLength(24); System.out.println(subnetMask); // 輸出:255.255.255.0
總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
JavaSwing FlowLayout 流式布局的實現(xiàn)
這篇文章主要介紹了JavaSwing FlowLayout 流式布局的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-12-12
詳解在springboot中使用Mybatis Generator的兩種方式
這篇文章主要介紹了詳解在springboot中使用Mybatis Generator的兩種方式,本文將介紹到在springboot的項目中如何去配置和使用MBG以及MBG生成代碼的兩種方式,非常具有實用價值,需要的朋友可以參考下2018-11-11
Java中使用JWT生成Token進行接口鑒權(quán)實現(xiàn)方法
這篇文章主要介紹了Java中使用JWT生成Token進行接口鑒權(quán)實現(xiàn)方法,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-08-08
Java Spring WEB應(yīng)用實例化如何實現(xiàn)
這篇文章主要介紹了Java Spring WEB應(yīng)用實例化如何實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-12-12
IDEA2020.1使用LeetCode插件運行并調(diào)試本地樣例的方法詳解
這篇文章主要介紹了IDEA2020.1使用LeetCode插件運行并調(diào)試本地樣例的方法,本文通過圖文并茂的形式給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友參考下吧2020-09-09
Spring?MVC中的Controller進行單元測試的實現(xiàn)
本文主要介紹了如何對Spring?MVC中的Controller進行單元測試的實現(xiàn),文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-02-02
Spring Cloud Alibaba和Dubbo融合實現(xiàn)
這篇文章主要介紹了Spring Cloud Alibaba和Dubbo融合實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-04-04
SpringBoot設(shè)置Json返回字段為非空問題
這篇文章主要介紹了SpringBoot設(shè)置Json返回字段為非空問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-08-08

