风扬  

public String getUnixLocalIp() {
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();
System.exit(-1);
}
return ip;
}

posted on 2014-11-26 16:14  风扬扬  阅读(392)  评论(0编辑  收藏  举报