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)文章
SpringBoot http post請求數(shù)據(jù)大小設(shè)置操作
這篇文章主要介紹了SpringBoot http post請求數(shù)據(jù)大小設(shè)置操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-09-09Mybatis配置錯誤:java.lang.ExceptionInInitializerError
這篇文章主要介紹了Mybatis配置錯誤:java.lang.ExceptionInInitializerError的相關(guān)資料,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-12-12MyBatis limit分頁設(shè)置的實現(xiàn)
這篇文章主要介紹了MyBatis limit分頁設(shè)置的實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04springboot+mybatis攔截器方法實現(xiàn)水平分表操作
這篇文章主要介紹了springboot+mybatis攔截器方法實現(xiàn)水平分表操作,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價值,需要的朋友可以參考一下2022-08-08SpringBoot使用開發(fā)環(huán)境application.properties問題
這篇文章主要介紹了SpringBoot使用開發(fā)環(huán)境application.properties問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-07-07