This article describes the Java to determine whether the IP address of the intranet IP or public network IP method. Share to everyone for your reference. The specific analysis is as follows:
In the TCP/IP protocol, three IP address areas are reserved for private addresses, and their addresses range as follows:
10.0.0.0/8:10.0.0.0~10.255.255.255
172.16.0.0/12:172.16.0.0~172.31.255.255
192.168.0.0/16:192.168.0.0~192.168.255.255
So, go directly to the code:
Copy Code code as follows:
public static Boolean internalip (String IP) {
byte[] addr = IPADDRESSUTIL.TEXTTONUMERICFORMATV4 (IP);
return Internalip (addr);
}
public static Boolean Internalip (byte[] addr) {
Final byte B0 = addr[0];
Final byte B1 = addr[1];
10.x.x.x/8
Final byte section_1 = 0x0A;
172.16.x.x/12
Final byte section_2 = (byte) 0xAC;
Final byte section_3 = (byte) 0x10;
Final byte Section_4 = (byte) 0x1F;
192.168.x.x/16
Final byte section_5 = (byte) 0xc0;
Final byte section_6 = (byte) 0xa8;
Switch (B0) {
Case SECTION_1:
return true;
Case Section_2:
if (B1 >= section_3 && B1 <= section_4) {
return true;
}
Case Section_5:
Switch (B1) {
Case Section_6:
return true;
}
Default
return false;
}
}
I hope this article will help you with your Java programming.