I. NetworkInterface concept
Java provides a NetworkInterface class. This class can get information about all the physical network interfaces of the machine and the logical network interfaces that the virtual machines and other software use to create the physical network interface of the machine.
Step One: NetworkInterface enumerates all of the network interfaces of the machine by means of the Getnetworkinterfaces method.
Step two: Traverse all network IPs, get native IP
Two. Java implementation
Private Staticstring Getlocalip () {string SIP=NULL; InetAddress IP=NULL; BooleanBfindip =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 (); //----------specific situations, consider using Ni.getname to judge//Traverse all IPsenumeration<inetaddress> ips =ni.getinetaddresses (); while(Ips.hasmoreelements ()) {IP=(inetaddress) ips.nextelement (); if(Ip.issitelocaladdress () &&!ip.isloopbackaddress ()//127. Start with Lookback address&& ip.gethostaddress (). IndexOf (":") = =-1) {Bfindip=true; Break; } } } if(IP! =NULL) {SIP=ip.gethostaddress (); } returnsip; }
Three. js distinguish inside and outside the network
functionGetipnum (ipAddress) {varIP =NewArray (); IP= Ipaddress.split ("."); varA = parseint (ip[0]); varb = parseint (ip[1]); varc = parseint (ip[2]); varD = parseint (ip[3]); returnA * * * * * * * * * * * * * * * + + c * 256 +D; }//is the intranetfunctionIsinnerip (IP) {varA1 = Getipnum ("10.0.0.0"); varA2 = Getipnum ("10.255.255.255"); varB1 = Getipnum ("172.16.0.0"); varB2 = Getipnum ("172.31.255.255"); varC1 = Getipnum ("192.168.0.0"); varC2 = Getipnum ("192.168.255.255"); varD1 = Getipnum ("10.44.0.0"); varD2 = Getipnum ("10.69.0.255"); varn =getipnum (IP); return(n >= A1 && n <= a2) | | (N >= b1 && n <= b2) | | (n >= C1 && n <= c2) | | (n >= d1 && n <=D2);}/** * * * intercept URL Request parameters * * @param paras * @returns*/functionGetrequesturl (paras) {varURL =Location.href; varparastring = url.substring (Url.indexof ("?") + 1, url.length). Split ("&"); varParaobj = {} for(i = 0; j = parastring[i]; i++) {paraobj[j.substring (0, J.indexof ("=")). toLowerCase ()] =j.substring (J. IndexOf ("=") + 1, j.length); } varReturnValue =paraobj[paras.tolowercase ()]; if(typeof(returnvalue) = = "Undefined") { return""; } Else { returnreturnvalue; }}
NetworkInterface get the host IP, judge the internal and external network