首页 > 代码库 > 推断给定的IP地址是否是内网IP
推断给定的IP地址是否是内网IP
/** * 推断给定的IP地址是否是内网IP * * @author GaoHuanJie */ public class Test{ public boolean isInnerIP(String ipAddress){/*推断是否是内网IP*/ boolean isInnerIp = false;//默认给定IP不是内网IP 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 * D类 127.0.0.0 -127.255.255.255(环回地址) **/ 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"); long dBegin = getIpNum("127.0.0.0"); long dEnd = getIpNum("127.255.255.255"); isInnerIp = isInner(ipNum,aBegin,aEnd) || isInner(ipNum,bBegin,bEnd) || isInner(ipNum,cBegin,cEnd) || isInner(ipNum,dBegin,dEnd); return isInnerIp; } private long getIpNum(String ipAddress) {/*获取IP数*/ String [] ip = ipAddress.split("\\."); 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; } private boolean isInner(long userIp,long begin,long end){ return (userIp>=begin) && (userIp<=end); } public static void main(String[] args) { if (new Test().isInnerIP("22.8.129.60")) {//172.16.0.119 System.out.println("该IP地址为内网IP!"); } else { System.out.println("该IP地址为外网IP!"); } } }
推断给定的IP地址是否是内网IP
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。