java 怎样根据IP来判断其是否存在某个网段内
答案:2 悬赏:80
解决时间 2021-03-12 13:29
- 提问者网友:说不出醉人情话
- 2021-03-12 02:29
java 怎样根据IP来判断其是否存在某个网段内
最佳答案
- 二级知识专家网友:樣嘚尐年
- 2021-03-12 03:20
使用正则表达式判断匹配
Pattern p=Pattern.compile(Regex);
Matcher matcher=p.matcher(TestStr);
if (matcher.find()) {
}
Pattern p=Pattern.compile(Regex);
Matcher matcher=p.matcher(TestStr);
if (matcher.find()) {
}
全部回答
- 1楼网友:浪者不回头
- 2021-03-12 03:37
java判断ip是否在指定网段中,看代码 public static boolean ipisinnet(string iparea, string ip) { if (iparea == null) throw new nullpointerexception("ip段不能为空!"); if (ip == null) throw new nullpointerexception("ip不能为空!"); iparea = iparea.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)"; final string regx_ipb = regx_ip + "//-" + regx_ip; if (!iparea.matches(regx_ipb) || !ip.matches(regx_ip)) return false; int idx = iparea.indexof('-'); string[] sips = iparea.substring(0, idx).split("//."); string[] sipe = iparea.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; } ok,祝君好运!
我要举报
如以上问答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯