ip校验方法:判断ip是否位于指定的范围内
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class IPvalidate
{
/**
* @param args
*/
public static void main(String[] args)
{
//存放ip对象的list,ip对象包含:开始段、结束段
List<IpDo> ipList = new ArrayList<IpDo>();
//第一行ip段
IpDo ipDo = new IpDo();
ipDo.setBeginIp("10.166.37.0");
ipDo.setEndIp("10.166.37.180");
ipList.add(ipDo);
//第二行ip段
IpDo ipDo2 = new IpDo();
ipDo2.setBeginIp("10.166.47.100");
ipDo2.setEndIp("10.166.47.200");
ipList.add(ipDo2);
//第三行ip段
IpDo ipDo3 = new IpDo();
ipDo3.setBeginIp("10.166.37.150");
ipDo3.setEndIp("10.166.37.255");
ipList.add(ipDo3);
// 校验:ip格式
if (!validatorIpFormat(ipList))
{
return;
}
// 校验:开始ip小于等于结束ip
if (!validatorStartIpLessThanEndIp(ipList))
{
return;
}
// 校验:判断IP段是否存在交集或包含关系 下标0 true or false 下标1 存在IP交集的对象
Object[] valResult = isHaveIntersection(ipList);
boolean flag = "true".equals(valResult[0].toString());
if (flag)
{
String intersectionStr = (String)valResult[1];
System.out.println("以下ip段存在交集,行号为:\n" + intersectionStr);
return;
}
}
/**
* 校验:ip格式
*/
public static boolean validatorIpFormat(List<IpDo> ipList)
{
int size = ipList.size();
for (int i = 0; i < size; i++)
{
String pattern = "([1-9]|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])(\\.(\\d|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])){3}";
Matcher startMat = Pattern.compile(pattern).matcher(ipList.get(i)
.getBeginIp());
Matcher endMat = Pattern.compile(pattern).matcher(ipList.get(i)
.getEndIp());
if (!startMat.matches() || !endMat.matches())
{
int n = i + 1;
System.out.println("第" + n + "行 ip格式不合法");
return false;
}
}
return true;
}
/**
* 校验:开始ip小于等于结束ip
*/
public static boolean validatorStartIpLessThanEndIp(List<IpDo> ipList)
{
boolean tag = true;
int i = 0;
try
{
int size = ipList.size();
// 校验ip,后者>=前者
for (i = 0; i < size; i++)
{
if (isLarger(ipList.get(i).getEndIp(), ipList.get(i)
.getBeginIp()))
{
int n = i + 1;
System.out.println("第" + n + "行 ip结束段应该大于开始段");
tag = false;
break;
}
}
}
catch (NumberFormatException e)
{
tag = false;
int n = i + 1;
System.out.println("第" + n + "行 ip转化异常");
}
return tag;
}
/**
* 判断 ip2 是否大于 ip1,若大于返回true,否则返回false
* @param ip1 ip1
* @param ip2 ip2
* @return boolean boolean
*/
public static boolean isLarger(String ip1, String ip2)
throws NumberFormatException
{
boolean flag = false;
String[] startips = ip1.split("\\.");
String[] endIps = ip2.split("\\.");
for (int i = 0; i < startips.length; i++)
{
if (Integer.parseInt(endIps[i]) > Integer.parseInt(startips[i]))
{
flag = true;
break;
}
else
{
if (Integer.parseInt(endIps[i]) == Integer.parseInt(startips[i]))
{
continue;
}
else
{
break;
}
}
}
return flag;
}
/**
* 判断提交的区域配置中,是否有存在IP段有交集的区域
* @return 下标0 true or false 下标1 存在IP交集的对象
*/
private static Object[] isHaveIntersection(List<IpDo> ipList)
{
//下标0 true or false 下标1 存在IP交集的Map对象
Object[] obj = new Object[2];
//默认不存在交集
obj[0] = false;
//存在交集的区域名字符串,用于界面提醒
StringBuffer buf = new StringBuffer();
int size = ipList.size();
for (int i = 0; i < size - 1; i++)
{
IpDo temp = ipList.get(i);
for (int j = i + 1; j < size; j++)
{
IpDo tempj = ipList.get(j);
if (isBetweenIpSeg(temp.getBeginIp(), tempj, ".")
|| isBetweenIpSeg(tempj.getBeginIp(), temp, "."))
{
obj[0] = true;
buf.append(i + 1);
buf.append('-');
buf.append(j + 1);
buf.append(',');
}
}
}
if (buf.length() > 0)
{
buf = buf.deleteCharAt(buf.length() - 1);
}
obj[1] = buf.toString();
return obj;
}
/**
* 判断IP是否在IP段内
* @param strIp 需判断的IP
* @param regionConfigDo IP段
* @param ipType ipv4:'.'分割 ipv6:':'分割
* @return 在段内'true' 不在段内'false'
*/
private static boolean isBetweenIpSeg(String strIp, IpDo ipDo, String ipType)
{
if (null == ipType)
{
return true;
}
long ipNumber = parseIpToNumber(strIp);
long startIpNumber = parseIpToNumber(ipDo.getBeginIp());
long endIpNumber = parseIpToNumber(ipDo.getEndIp());
if (startIpNumber > ipNumber || ipNumber > endIpNumber)
{
//无交集
return false;
}
return true;
}
/**
* 将IPV4的IP转换成Long
* @param ipStr Ip
* @return IP Number
*/
private static long parseIpToNumber(String ipStr)
{
/** IP进制转换值(256)*/
final long IPHEXNUM = 256L;
long ipNumber = 0L;
String[] ips = ipStr.split("\\.");
ipNumber = ipNumber + Integer.parseInt(ips[0]) * IPHEXNUM * IPHEXNUM
* IPHEXNUM;
ipNumber = ipNumber + Integer.parseInt(ips[1]) * IPHEXNUM * IPHEXNUM;
ipNumber = ipNumber + Integer.parseInt(ips[2]) * IPHEXNUM;
ipNumber = ipNumber + Integer.parseInt(ips[3]);
return ipNumber;
}
}
ip校验方法:判断ip是否位于指定的范围内的更多相关文章
- C# 判断ip地址是否正确
最后要用一方法判断ip地址是否正确,直接用.Net现成的类,方法如下: string ipStr="192.168.222.333"; IPAddress ip; if(IPAdd ...
- 最全,可直接用的一些正则校验,判断邮箱,手机号码,车牌号,身份证号,网址,账号,密码,ip,去掉html格式,工商税号等。
一些正则校验,判断邮箱,手机号码,车牌号,身份证号,网址,账号,密码,ip,去掉html格式,工商税号等. // 判断邮箱 isValid = [text isValidEmail]; // 判断手机 ...
- SSRF绕过IP限制方法总结
SSRF绕过IP限制方法总结 - Summary of SSRF methods for bypassing IP restrictions -https://www.cnblogs.com/iAmS ...
- 为Linux服务器设置静态IP的方法
这里以CentOS 7系列为例设置静态IP,原来RedHat系列的Linux发行版可以通过setup工具方便的设置静态IP,但是在版本7之后setup工具的功能就逐渐减弱了,所以这时候采用修改配置文件 ...
- js获取IP地址方法总结_转
js代码获取IP地址的方法,如何在js中取得客户端的IP地址.原文地址:js获取IP地址的三种方法 http://www.jbxue.com/article/11338.html 1,js取得IP地址 ...
- (转载)php如何判断IP为有效IP地址
(转载)http://www.kuitao8.com/20130918/1376.shtml 多数人看到这篇日志,第一印象肯定是以为是要讲如何通过正则表达式来判断. 非也,在php5.2.0之后,有专 ...
- PHP判断ip地址是否合法
1.获取真正ip地址 function get_ip(){ //判断服务器是否允许$_SERVER if(isset($_SERVER)){ if(isset($_SERVER[HTTP_X_FORW ...
- 服务器安全策略之《通过IP安全策略阻止某个IP访问的设置方法》
现在我们在布署好了一个网站,发布到外网后就意味着将会接受来自四面八方的黑客攻击,这个情况很常见,我们的网站基本上每天都要接受成千上万次的攻击,有SQL注入的.有代码注入的.有CC攻击等等...而我作为 ...
- C# 正则表达式判断IP,URL等及其解释
C# 正则表达式判断IP,URL等及其解释 判断IP格式方法: public static bool ValidateIPAddress(string ipAddress) { Regex valid ...
随机推荐
- 10个经典的C语言面试基础算法及代码
10个经典的C语言面试基础算法及代码作者:码农网 – 小峰 原文地址:http://www.codeceo.com/article/10-c-interview-algorithm.html 算法是一 ...
- 4.27-4.30webstorm
本周学习了html的基础课程,运用的软件是webstorm,网页的结构大体为: <html><head> My First Heading </head> < ...
- 数据库知识整理<五>
简单的数据查询: 5.1查询的基本结构: Sql语句:select [distinct] (* | column [alias],...) from table [where condition] [ ...
- We have detected that MySQL products under the Commercial license are installed. In order to proceed with this GPL installation these Commercial
下载了MySQL 5.6.15,在安装时,出现了下面的提示信息: 按提示信息的要求单击“是”,结果安装就无法进行下去. 从提示信息上看,意思是指电脑中原来安装有商业版的许可,现在要转换成为GPL许可. ...
- 需要弥补的那部分SQL
一.前言 虽然我们大多数人都学习过SQL,但是经常忽略它.总是会自以为学到的已经足够用了,从而导致我们在实际开发的过程中遇到复杂的问题后只能在检索数据后通过传统的代码来完成,但是其中很多的功能利用SQ ...
- iis php5.3.8 默认文档无效 404 - 找不到文件或目录
环境:WIN2008 R2 IIS7.5 / .NET4.X 新开1站点,使用php(5.3.8),默认首页文档已设置为index.php,网站所在目录的网站运行时用户权限正确,应用程序池是asp.n ...
- Windows下安装MinGW,编译c/c++时出现cannot find -lpthread解决办法
由于Mingw下没有带pthread库,所以在eclipse中设置多线程动态链接库,也不管用.需要自己下载, ftp://sourceware.org/pub/pthreads-win32/pthre ...
- VR介绍
VR(Virtual Reality,即虚拟现实,简称VR),是由美国VPL公司创建人拉尼尔在20世纪80年代初提出的.其具体内涵是:综合利用计算机图形系统和各种现实及控制等接口设备,在计算机上生成的 ...
- Nginx运行Mono Web (ASP.NET)
Mono Web除了可以使用Apache/mod_mono方式承载运行外,还可以使用Nginx/FastCGI方式运行. Nginx配置asp.net更简单方便,用处也多,可以通过FastCGI运行a ...
- paip.提升效率---request自动绑定domain object
paip.提升效率---request自动绑定domain object #.keyword,subtitle关键字,子标题 ------------------------- 复制request属性 ...