Java网络相关
1.获得本机IP
参考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; }
可以用ip.isSiteLocalAddress() 来判断是内网IP还是外网IP