62,634
社区成员




package common.util.ip;
import common.util.ip.exception.IPFormatException;
public interface IPValidator {
/**
* 根据传入的源IP地址和目的IP地址进行匹配检验
* @param {String} srcIP --源IP地址
* @param {String} destIP --目的IP
* @throws IPFormatException
*/
public boolean singleValidate(String srcIP, String destIP) throws IPFormatException;
/**
* 根据传入的源IP地址和目的IP地址进行匹配检验
* @param {String[]} multipleIP --源IP地址组
* @param {String} destIP --目的IP(只能是一个IP)
* @throws IPFormatException
*/
public boolean multipleValidate(String[] multipleIP, String destIP) throws IPFormatException;
/**
* 根据传入的源IP地址和目的IP地址进行匹配检验
* @param {String} regionStartIP --源IP起始地址
* @param {String} regionEndIP --源IP结束地址
* @param {String} destIP --目的IP
* @throws IPFormatException
*/
public boolean regionValidate(String regionStartIP, String regionEndIP, String destIP) throws IPFormatException;
}
package common.util.ip.impl;
import common.util.ip.IPFormatChecker;
import common.util.ip.IPValidator;
import common.util.ip.exception.IPFormatException;
/**
* IPv4校验类
* @author 莫取网名
*/
class IPv4Validator implements IPValidator{
public boolean singleValidate(String srcIP, String destIP) throws IPFormatException{
if(IPFormatChecker.checkIPv4(srcIP) && IPFormatChecker.checkIPv4(destIP)){
return destIP.equals(srcIP);
}
return false;
}
public boolean multipleValidate(String[] multipleIP, String destIP) throws IPFormatException {
if(!IPFormatChecker.checkIPv4(destIP))
return false;
int ipCount=multipleIP.length;
for(int i=0;i<ipCount;i++){
if(IPFormatChecker.checkIPv4(multipleIP[i])){
if(destIP.equals(multipleIP[i])){
return true;
}
}
}
return false;
}
public boolean regionValidate(String regionStartIP, String regionEndIP, String destIP) throws IPFormatException {
if(!IPFormatChecker.checkIPv4(regionStartIP))
return false;
if(!IPFormatChecker.checkIPv4(regionEndIP))
return false;
if(!IPFormatChecker.checkIPv4(destIP))
return false;
//由于.号在正则表达式中是特殊符号,所以split的参数需要进行转义
String[] sSectionStartIPNums = regionStartIP.split("\\."); // 得到源IP段起始地址的4位数字串
String[] sSectionEndIPNums = regionEndIP.split("\\."); // 得到源IP段结束地址的4位数字串
String[] sDestIPNums = destIP.split("\\."); //得到目的IP地址的4位数字串
long ipSectionStart = 0L, ipSectionEnd = 0L, ipDest = 0L;
for (int i = 0; i < 4; i++) {//将三个IP的数字串转换成32位的长整型数据
ipSectionStart = ipSectionStart << 8 | Integer.parseInt(sSectionStartIPNums[i]);
ipSectionEnd = ipSectionEnd << 8 | Integer.parseInt(sSectionEndIPNums[i]);
ipDest = ipDest << 8 | Integer.parseInt(sDestIPNums[i]);
}
//如果起始IP大于结束IP,则进行交换
if (ipSectionStart > ipSectionEnd) {
long temp = ipSectionStart;
ipSectionStart = ipSectionEnd;
ipSectionEnd = temp;
}
return ipSectionStart <= ipDest && ipDest <= ipSectionEnd;
}
}
package common.util.ip.impl;
import common.util.ip.IPValidator;
/**
* IP检验类工厂
* @author 莫取网名
*/
public class IPValidatorFactory {
public static IPValidator getInstance(String className) {
IPValidator ipValidator = null;
try {
// 利用反射获取类型
ipValidator = (IPValidator) Class.forName(className).newInstance();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
return ipValidator;
}
}
package common.util.ip.exception;
/**
* IP格式异常类
* @author 莫取网名
*
*/
public class IPFormatException extends Exception {
private static final long serialVersionUID = 2018923238888254267L;
public IPFormatException() {}
public IPFormatException(String message) {
super(message);
}
public IPFormatException(Throwable cause) {
super(cause);
}
public IPFormatException(String message, Throwable cause) {
super(message, cause);
}
}
package common.util.ip;
import common.util.ip.exception.IPFormatException;
/**
* IP格式检查类
* @author 莫取网名
*/
public class IPFormatChecker {
// private final static String IP_V4_REGULAR = "^((25[0-5]|2[0-4]\\d|[0-1]?\\d\\d?)\\.){3}(25[0-5]|2[0-4]\\d|[0-1]?\\d\\d?)$";
private final static String IP_V4_REGULAR = "((25[0-5]|2[0-4]\\d|1\\d{2}|[1-9]\\d|\\d)\\.){3}(25[0-5]|2[0-4]\\d|1\\d{2}|[1-9]\\d|\\d)";
private final static String IP_V6_REGULAR = ""; //尚未定义(可自行定义)
/**
* 检查某个IPv4的格式是否正确
* @param {String} ip
* @return {boolean}
* @throws IPFormatException
*/
public static boolean checkIPv4(String ip) throws IPFormatException{
if(ip==null || "".equals(ip)){
throw new IPFormatException("IPv4地址不能为空!");
}
if(!ip.matches(IP_V4_REGULAR)){
throw new IPFormatException("IPv4["+ip+"]格式或内容不正确!");
}
return true;
}
/**
* 检查某个IPv6的格式是否正确
* @param {String} ip
* @return {boolean}
* @throws IPFormatException
*/
public static boolean checkIPv6(String ip) throws IPFormatException{
if(ip==null || "".equals(ip)){
throw new IPFormatException("IPv6地址不能为空!");
}
if(!ip.matches(IP_V6_REGULAR)){
throw new IPFormatException("IPv6["+ip+"]格式或内容不正确!");
}
return false;
}
}
package common.test;
import common.util.ip.IPValidator;
import common.util.ip.exception.IPFormatException;
import common.util.ip.impl.IPValidatorFactory;
public class IPTester {
public static void main(String[] args) throws IPFormatException {
String destIP = "192.168.10.1120"; //目标IP
IPValidator ipValidator = (IPValidator) IPValidatorFactory.getInstance("common.util.ip.impl.IPv4Validator");
String singleIP = "192.168.10.116";
System.out.println(ipValidator.singleValidate(singleIP,destIP)); //测试单个IP
String manyIP = "192.168.10.119,192.168.10.116,192.168.10.118";
System.out.println(ipValidator.multipleValidate(manyIP.split(","),destIP)); //测试多个IP
String regionStartIP = "192.168.10.111", regionEndIP = "192.168.11.111";
System.out.println(ipValidator.regionValidate(regionStartIP, regionEndIP, destIP)); //测试区间IP
}
}