獲取真實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
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章