//判断某一个IP是否为内网IP
public static boolean isInnerIP(String ipAddress) {
boolean isInnerIp = false;
long ipNum = getIpNum(ipAddress);
/**
* 私有IP:A类 10.0.0.0-10.255.255.255 B类 172.16.0.0-172.31.255.255 C类
* 192.168.0.0-192.168.255.255 当然,还有127这个网段是环回地址
*/
long aBegin = getIpNum("10.0.0.0");
long aEnd = getIpNum("10.255.255.255");
long bBegin = getIpNum("172.16.0.0");
long bEnd = getIpNum("172.31.255.255");
long cBegin = getIpNum("192.168.0.0");
long cEnd = getIpNum("192.168.255.255");
isInnerIp =
isInner(ipNum, aBegin, aEnd)
|| isInner(ipNum, bBegin, bEnd)
|| isInner(ipNum, cBegin, cEnd)
|| ipAddress.equals("127.0.0.1");
return isInnerIp;
}
//把ip地址转换为数字
public static long getIpNum(String ipAddress) {
String[] ip = split(ipAddress, ".");
long a = Integer.parseInt(ip[0]);
long b = Integer.parseInt(ip[1]);
long c = Integer.parseInt(ip[2]);
long d = Integer.parseInt(ip[3]);
long ipNum = a * 256 * 256 * 256 + b * 256 * 256 + c * 256 + d;
return ipNum;
}
/**
* 判断指定IP是否在某一IP段内
* @param userIp 指定IP
* @param begin 开始IP
* @param end 结束IP
* @return 如果为true则在指定IP段内,否则返回false
*/
public static boolean isInner(long userIp, long begin, long end) {
return (userIp >= begin) && (userIp <= end);
}
/**
* 分割字符串,原理:检测字符串中的分割字符串,然后取子串
* @param original 需要分割的字符串
* @paran regex 分割字符串
* @return 分割后生成的字符串数组
* 例;
* split(String original,String "$%")
* 注意,后面的参数是表示分隔符,且"$%"表示的是$和%都可以是分隔符,并不是两者一起才是分隔符
*/
public static String[] split(String original, String regex) {
StringTokenizer st = new StringTokenizer(original, regex);
ArrayList al = new ArrayList();
while (st.hasMoreTokens()) {
al.add(st.nextToken());
}
String[] result = new String[al.size()];
return (String[]) al.toArray(result);
}
public static boolean isInnerIP(String ipAddress) {
boolean isInnerIp = false;
long ipNum = getIpNum(ipAddress);
/**
* 私有IP:A类 10.0.0.0-10.255.255.255 B类 172.16.0.0-172.31.255.255 C类
* 192.168.0.0-192.168.255.255 当然,还有127这个网段是环回地址
*/
long aBegin = getIpNum("10.0.0.0");
long aEnd = getIpNum("10.255.255.255");
long bBegin = getIpNum("172.16.0.0");
long bEnd = getIpNum("172.31.255.255");
long cBegin = getIpNum("192.168.0.0");
long cEnd = getIpNum("192.168.255.255");
isInnerIp =
isInner(ipNum, aBegin, aEnd)
|| isInner(ipNum, bBegin, bEnd)
|| isInner(ipNum, cBegin, cEnd)
|| ipAddress.equals("127.0.0.1");
return isInnerIp;
}
//把ip地址转换为数字
public static long getIpNum(String ipAddress) {
String[] ip = split(ipAddress, ".");
long a = Integer.parseInt(ip[0]);
long b = Integer.parseInt(ip[1]);
long c = Integer.parseInt(ip[2]);
long d = Integer.parseInt(ip[3]);
long ipNum = a * 256 * 256 * 256 + b * 256 * 256 + c * 256 + d;
return ipNum;
}
/**
* 判断指定IP是否在某一IP段内
* @param userIp 指定IP
* @param begin 开始IP
* @param end 结束IP
* @return 如果为true则在指定IP段内,否则返回false
*/
public static boolean isInner(long userIp, long begin, long end) {
return (userIp >= begin) && (userIp <= end);
}
/**
* 分割字符串,原理:检测字符串中的分割字符串,然后取子串
* @param original 需要分割的字符串
* @paran regex 分割字符串
* @return 分割后生成的字符串数组
* 例;
* split(String original,String "$%")
* 注意,后面的参数是表示分隔符,且"$%"表示的是$和%都可以是分隔符,并不是两者一起才是分隔符
*/
public static String[] split(String original, String regex) {
StringTokenizer st = new StringTokenizer(original, regex);
ArrayList al = new ArrayList();
while (st.hasMoreTokens()) {
al.add(st.nextToken());
}
String[] result = new String[al.size()];
return (String[]) al.toArray(result);
}