函数与提取:
public static boolean isSameAddress(String resourceIp, String requestIp);判断是否属于同一网段,默认子网掩码
public static boolean isSameAddress(String resourceIp, String requestIp, String subnetMask);判断是否属于同一网段,指定子网掩码
public static String getIpType(String ip);判断IP地址类型A、B、C类
package com.laiwei.stream.util;
import com.laiwei.framework.exception.RuleIllegalArgumentException;
import org.junit.Test;
public class IpV4Util {
public static final String DEFAULT_SUBNET_MASK_A = "255.0.0.0";
public static final String DEFAULT_SUBNET_MASK_B = "255.255.0.0";
public static final String DEFAULT_SUBNET_MASK_C = "255.255.255.0";
public static final String TYPE_IP_A = "A";
public static final String TYPE_IP_B = "B";
public static final String TYPE_IP_C = "C";
public static final String TYPE_IP_D = "D";
public static final String TYPE_IP_LOCATE = "locate";
//@Test
//public void test() {
//System.out.println(isSameAddress("192.168.1.1", "192.168.1.5", DEFAULT_SUBNET_MASK_C));
//
//}
@Test
public void test() {
String ip_a="127.0.01.15";
String ip_c="192.168.1.1";
String binaryIp = getBinaryIp("192.168.1.1");
System.out.println(getIpType(ip_a));
}
//判断ip是否属于同一网段,默认子网掩码
public static boolean isSameAddress(String resourceIp, String requestIp) {
if (getIpType(resourceIp).equals(getIpType(requestIp))){
return isSameAddress(resourceIp,requestIp,getIpDefaultMask(getIpType(resourceIp)));
}
return false;
}
//通过ip类型,获取默认IP子网掩码
private static String getIpDefaultMask(String ipType){
switch (ipType){
case TYPE_IP_A:return DEFAULT_SUBNET_MASK_A;
case TYPE_IP_B:return DEFAULT_SUBNET_MASK_B;
case TYPE_IP_C:return DEFAULT_SUBNET_MASK_C;
default:throw new RuleIllegalArgumentException("没有对应的mask地址");
}
}
//判断ip是否属于同一网段
public static boolean isSameAddress(String resourceIp, String requestIp, String subnetMask) {
String resourceAddr = getAddrIp(resourceIp, subnetMask);
String subnetMaskAddr = getAddrIp(requestIp, subnetMask);
if (resourceAddr.equals(subnetMaskAddr)) {
return true;
}
return false;
}
//获取ip的二进制字符串
private static String getBinaryIp(String data) {
String[] datas = data.split("\\.");
String binaryIp = "";
for (String ipStr : datas) {
long signIp = Long.parseLong(ipStr);
String binary = Long.toBinaryString(signIp);
long binaryInt = Long.parseLong(binary);
binaryIp += String.format("%08d", binaryInt);
}
return binaryIp;
}
//获取ip的地址位
private static String getAddrIp(String ip, String subnetMask) {
StringBuilder addrIp = new StringBuilder();
String binaryIp = getBinaryIp(ip);
String binarySubnetMask = getBinaryIp(subnetMask);
for (int i = 0; i < 32; i++) {
byte ipByte = Byte.parseByte(String.valueOf(binaryIp.charAt(i)));
byte subnetMaskByte = Byte.parseByte(String.valueOf(binarySubnetMask.charAt(i)));
addrIp.append(ipByte & subnetMaskByte);
}
return addrIp.toString();
}
//获取ip是什么类型,ABCD
public static String getIpType(String ip) {
String binaryIp = getBinaryIp(ip);
System.out.println(binaryIp);
if (ip.startsWith("127")){
return TYPE_IP_LOCATE;
}
if (binaryIp.startsWith("0")){
return TYPE_IP_A;
}
if (binaryIp.startsWith("10")){
return TYPE_IP_B;
}
if (binaryIp.startsWith("110")){
return TYPE_IP_C;
}
if (binaryIp.startsWith("1110")){
return TYPE_IP_D;
}
throw new RuleIllegalArgumentException("无效ip异常");
}
}