admin 管理员组

文章数量: 1184232

代码如下:

public static String getCurrentHostIpAddress() {
        Enumeration<NetworkInterface> n;
        try {
            n = NetworkInterface.getNetworkInterfaces();
            for (; n.hasMoreElements(); ) {
                NetworkInterface e = n.nextElement();
                Enumeration<InetAddress> a = e.getInetAddresses();
                for (; a.hasMoreElements(); ) {
                    InetAddress addr = a.nextElement();
                    String ipAddress = addr.getHostAddress();
                    if (!ipAddress.equals("127.0.0.1")
                            && ipAddress.indexOf(":") == -1) {
                        return ipAddress;
                    }
                }
            }
            throw new RuntimeException(
                    "Can't get the current host ip address.");
        } catch (SocketException e1) {
            throw new RuntimeException(
                    "Can't get the current host ip address:" + e1);
        }
    }

本文标签: 深度解析 中获取内 部和外部