标签:
一.NetworkInterface概念
Java提供了一个NetworkInterface类。这个类可以得到本机所有的物理网络接口和虚拟机等软件利用本机的物理网络接口创建的逻辑网络接口的信息。
第一步:NetworkInterface通过getNetworkInterfaces方法来枚举本机所有的网络接口。
第二步:遍历所有网络ip,获取本机ip
二.java实现
private static String getLocalIp() { String sip = null; InetAddress ip = null; boolean bFindIP = false; Enumeration<NetworkInterface> netInterfaces = null; try { netInterfaces = (Enumeration<NetworkInterface>) NetworkInterface.getNetworkInterfaces(); } catch (SocketException e) { e.printStackTrace(); } while (netInterfaces.hasMoreElements()) { if (bFindIP) { break; } NetworkInterface ni = (NetworkInterface) netInterfaces.nextElement(); // ----------特定情况,可以考虑用ni.getName判断 // 遍历所有ip Enumeration<InetAddress> ips = ni.getInetAddresses(); while (ips.hasMoreElements()) { ip = (InetAddress) ips.nextElement(); if (ip.isSiteLocalAddress() && !ip.isLoopbackAddress() // 127.开头的都是lookback地址 && ip.getHostAddress().indexOf(":") == -1) { bFindIP = true; break; } } } if (ip != null) { sip = ip.getHostAddress(); } return sip; }
三. js区分内外网
function getIpNum(ipAddress){ var ip = new Array(); ip= ipAddress.split("."); var a = parseInt(ip[0]); var b = parseInt(ip[1]); var c = parseInt(ip[2]); var d = parseInt(ip[3]); return a * 256 * 256 * 256 + b * 256 * 256 + c * 256 + d; } //是否为内网 function isInnerIP(ip){ var a1 = getIpNum("10.0.0.0"); var a2 = getIpNum("10.255.255.255"); var b1 = getIpNum("172.16.0.0"); var b2 = getIpNum("172.31.255.255"); var c1 = getIpNum("192.168.0.0"); var c2 = getIpNum("192.168.255.255"); var d1 = getIpNum("10.44.0.0"); var d2 = getIpNum("10.69.0.255"); var n = getIpNum(ip); return (n >= a1 && n <= a2) || (n >= b1 && n <= b2) || (n >= c1 && n <= c2) || (n >= d1 && n <= d2); } /****** * 截取URL请求参数 * * @param paras * @returns */ function getRequestURL(paras) { var url = location.href; var paraString = url.substring(url.indexOf("?") + 1, url.length).split("&"); var paraObj = {} for (i = 0; j = paraString[i]; i++) { paraObj[j.substring(0, j.indexOf("=")).toLowerCase()] = j.substring(j .indexOf("=") + 1, j.length); } var returnValue = paraObj[paras.toLowerCase()]; if (typeof (returnValue) == "undefined") { return ""; } else { return returnValue; } }
标签:
原文地址:http://www.cnblogs.com/zt528/p/3124207.html