淘先锋技术网

首页 1 2 3 4 5 6 7

代码示例:

/**

*获取Linux下的IP地址

*

*@returnIP地址

*@throwsSocketException

*/

publicstaticStringgetLinuxLocalIp()throwsSocketException{

Stringip="";

try{

for(Enumeration<NetworkInterface>en=NetworkInterface.getNetworkInterfaces();

en.hasMoreElements();){

NetworkInterfaceintf=en.nextElement();

Stringname=intf.getName();

if(!name.contains("docker")&&!name.contains("lo")){

for(Enumeration<InetAddress>enumIpAddr=intf.getInetAddresses();

enumIpAddr.hasMoreElements();){

InetAddressinetAddress=enumIpAddr.nextElement();

if(!inetAddress.isLoopbackAddress()){

Stringipaddress=inetAddress.getHostAddress().toString();

if(!ipaddress.contains("::")&&!ipaddress.contains("0:0:")

&&!ipaddress.contains("fe80")){

ip=ipaddress;

}

}

}

}

}

}catch(SocketExceptionex){

System.out.println("获取ip地址异常");

ex.printStackTrace();

}

System.out.println("IP:"+ip);

returnip;

}