获取真实IP地址 获取eth0 IP

获取真实IP 地址方法 :此方法获取linux 下 eth0 地址

	/**
* 获取本机IP
*/
public static String getLocalIP() {
String ip = "";
try {
if(isLinux()){
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;
}
}
}else{
ip = InetAddress.getLocalHost().getHostAddress().toString();
}

} catch (Exception e) {
e.printStackTrace();
}

return ip;
}



参考 :http://rylan.iteye.com/blog/654345

你可能感兴趣的:(java,备忘)