首页 > 代码库 > 获取本机IP(适用于Linux系统)

获取本机IP(适用于Linux系统)

获取本机IP(适用于Linux系统)
/**
* @desc 获取本机IP(适用于Linux系统)
* @return Ip
*/
public static String getLocalIP() {
String ip = "";
try {
Enumeration<?> e1 = (Enumeration<?>) NetworkInterface
.getNetworkInterfaces();
while (e1.hasMoreElements()) {
NetworkInterface ni = (NetworkInterface) e1.nextElement();
if (!ni.getName().equals("eth0")) {
continue;
} else {
Enumeration<?> e2 = ni.getInetAddresses();
while (e2.hasMoreElements()) {
InetAddress ia = (InetAddress) e2.nextElement();
if (ia instanceof Inet6Address)
continue;
ip = ia.getHostAddress();
}
break;
}
}
} catch (SocketException e) {
e.printStackTrace();
}
return ip;
}
 
原创

获取本机IP(适用于Linux系统)