首页 > 系统服务 > 详细

获得本机ip linux

时间:2016-08-02 18:47:50      阅读:377      评论:0      收藏:0      [点我收藏+]
/**
* 获得本机执行的ip
* @return
*/
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) {
logger.error("获取本机ip error");
}
return ip;
}

获得本机ip linux

原文:http://www.cnblogs.com/userrain/p/5730203.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!