/*** Take current system site local address Linux under and window available * *@return */ Public Staticstring Getlocalip () {string SIP= ""; InetAddress IP=NULL; Try { //if the Windows operating system if(Iswindowsos ()) {IP=Inetaddress.getlocalhost (); } //if the Linux operating system Else { BooleanBfindip =false; Enumeration<NetworkInterface> netinterfaces =networkinterface. getnetworkinterfaces (); while(Netinterfaces.hasmoreelements ()) {if(BFINDIP) { Break; } networkinterface ni=netinterfaces.nextelement (); //----------specific situations, consider using Ni.getname to judge//Traverse all IPsenumeration<inetaddress> ips =ni.getinetaddresses (); while(Ips.hasmoreelements ()) {IP=ips.nextelement (); if(Ip.issitelocaladdress () &&!ip.isloopbackaddress ()//127. Start with Lookback address&& ip.gethostaddress (). IndexOf (":") = =-1) {Bfindip=true; Break; } } } } } Catch(Exception e) {e.printstacktrace (); } if(NULL!=IP) {SIP=ip.gethostaddress (); } returnSIP; } Public Static BooleanIswindowsos () {if("//". Equals (File.separator)) { return true; } Else { return false; } }
Java gets the native IP address