java获取服务器ip地址解决linux上为127.0.0.1的问题

try {
            Enumeration networkInterfaces = NetworkInterface.getNetworkInterfaces();
            while (networkInterfaces.hasMoreElements()) {
                NetworkInterface ni = (NetworkInterface) networkInterfaces.nextElement();
                Enumeration nias = ni.getInetAddresses();
                while (nias.hasMoreElements()) {
                    InetAddress ia = (InetAddress) nias.nextElement();
                    if (!ia.isLinkLocalAddress() && !ia.isLoopbackAddress() && ia instanceof Inet4Address) {
                        hostIP=ia.getHostAddress();
                    }
                }
            }
        } catch (SocketException e) {
        	e.printStackTrace();
        }

  

转载于:https://www.cnblogs.com/chaojibaidu/p/10965992.html

你可能感兴趣的:(java获取服务器ip地址解决linux上为127.0.0.1的问题)