1. 判断某一IP地址是否在某一个IP段内
//判断客户端ip是否在指定ip段范围内---与一个ip段比较
public static boolean ipIsValid(String ipSection, String ip) {
if (ipSection == null)
throw new NullPointerException("IP段不能为空!");
if (ip == null)
throw new NullPointerException("IP不能为空!");
ipSection = ipSection.trim();
ip = ip.trim();
final String REGX_IP = "((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)"; //合法的ip格式
final String REGX_IPB = REGX_IP + "\\-" + REGX_IP;
if (!ipSection.matches(REGX_IPB) || !ip.matches(REGX_IP)) //判断ip或者ip段是否合法
return false;
int idx = ipSection.indexOf('-');
String[] sips = ipSection.substring(0, idx).split("\\.");
String[] sipe = ipSection.substring(idx + 1).split("\\.");
String[] sipt = ip.split("\\.");
long ips = 0L, ipe = 0L, ipt = 0L;
for (int i = 0; i < 4; ++i) {
ips = ips << 8 | Integer.parseInt(sips[i]);
ipe = ipe << 8 | Integer.parseInt(sipe[i]);
ipt = ipt << 8 | Integer.parseInt(sipt[i]);
}
if (ips > ipe) {
long t = ips;
ips = ipe;
ipe = t;
}
return ips <= ipt && ipt <= ipe;
}
2. 判断某一IP地址是否在某几个IP段内
//判断客户端ip是否在指定ip段范围内---与多个ip段(ipSections)比较
public static boolean ipIsValids(String ipSections, String ip) {
boolean flag=false;
if (ipSections == null)
throw new NullPointerException("IP段不能为空!");
if (ip == null)
throw new NullPointerException("IP不能为空!");
ipSections = ipSections.trim();
ip = ip.trim();
String[] ipSection=ipSections.split(",");
final String REGX_IP = "((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)"; //合法的ip格式
final String REGX_IPB = REGX_IP + "\\-" + REGX_IP;
if(!ip.matches(REGX_IP)){
return flag;
}
String[] sipt = ip.split("\\.");
for(int i=0;i
if(!ipSection[i].matches(REGX_IPB) && !ipSection[i].matches(REGX_IP)) //判断ip或者ip段是否合法
return flag;
int idx = ipSection[i].indexOf('-');
String[] sips = ipSection[i].substring(0, idx).split("\\.");
String[] sipe = ipSection[i].substring(idx + 1).split("\\.");
long ips = 0L, ipe = 0L, ipt = 0L;
for (int j = 0; j < 4; ++j) {
ips = ips << 8 | Integer.parseInt(sips[j]);
ipe = ipe << 8 | Integer.parseInt(sipe[j]);
ipt = ipt << 8 | Integer.parseInt(sipt[j]);
}
if (ips > ipe) {
long t = ips;
ips = ipe;
ipe = t;
}
if(ips <= ipt && ipt <= ipe){
flag=true;
break;
}else {
continue;
}
}
return flag;
}
3. 判断某一IP地址是否在某一个或几个ip段或者等于制定的某个ip的范围内
//判断客户端ip是否在指定ip段范围内---与多个ip段以及多个单一ip比较
public static boolean ipIsValids(String ipSections, String ip) {
boolean flag=false;
if (ipSections == null)
throw new NullPointerException("IP段不能为空!");
if (ip == null)
throw new NullPointerException("IP不能为空!");
ipSections = ipSections.trim();
ip = ip.trim();
String[] ipSection=ipSections.split(",");
final String REGX_IP = "((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)"; //合法的ip格式
final String REGX_IPB = REGX_IP + "\\-" + REGX_IP;
if(!ip.matches(REGX_IP)){
return flag;
}
String[] sipt = ip.split("\\.");
for(int i=0;i
if(!ipSection[i].matches(REGX_IPB) && !ipSection[i].matches(REGX_IP)) //判断ip或者ip段是否合法
return flag;
if(ipSection[i].matches(REGX_IP)){
flag=ip.equals(ipSection[i]);
}else {
int idx = ipSection[i].indexOf('-');
String[] sips = ipSection[i].substring(0, idx).split("\\.");
String[] sipe = ipSection[i].substring(idx + 1).split("\\.");
long ips = 0L, ipe = 0L, ipt = 0L;
for (int j = 0; j < 4; ++j) {
ips = ips << 8 | Integer.parseInt(sips[j]);
ipe = ipe << 8 | Integer.parseInt(sipe[j]);
ipt = ipt << 8 | Integer.parseInt(sipt[j]);
}
if (ips > ipe) {
long t = ips;
ips = ipe;
ipe = t;
}
if(ips <= ipt && ipt <= ipe){
flag=true;
break;
}else {
continue;
}
}
}
return flag;
}
可参考文档:https://blog.csdn.net/whatzhang007/article/details/113601349#comments_18384389