欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Java如何判斷一個IP是否在給定的網(wǎng)段內(nèi)

 更新時間:2025年04月30日 09:51:38   作者:Roc.Chang  
這篇文章主要介紹了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)文章

最新評論