獲取本機IP(適用於Linux系統)
阿新 • • 發佈:2017-08-07
tex cat 獲取本機 smo cal network enum print inet6 獲取本機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系統)