标签:
参考http://stackoverflow.com/questions/2381316/java-inetaddress-getlocalhost-returns-127-0-0-1-how-to-get-real-ip
public InetAddress getCurrentIp() { try { Enumeration<NetworkInterface> networkInterfaces = NetworkInterface .getNetworkInterfaces(); while (networkInterfaces.hasMoreElements()) { NetworkInterface ni = (NetworkInterface) networkInterfaces .nextElement(); Enumeration<InetAddress> nias = ni.getInetAddresses(); while(nias.hasMoreElements()) { InetAddress ia= (InetAddress) nias.nextElement(); if (!ia.isLinkLocalAddress() && !ia.isLoopbackAddress() && ia instanceof Inet4Address) { return ia; } } } } catch (SocketException e) { LOG.error("unable to get current IP " + e.getMessage(), e); } return null; }
标签:
原文地址:http://www.cnblogs.com/majia1949/p/4505912.html