linux下使用Java获取本机ip地址?

2019独角兽企业重金招聘Python工程师标准>>> hot3.png

/**
     * Get host IP address
     *
     * @return IP Address
     */
    private static InetAddress getAddress() {
        try {
            for (Enumeration interfaces = NetworkInterface.getNetworkInterfaces(); interfaces.hasMoreElements();) {
                NetworkInterface networkInterface = interfaces.nextElement();
                if (networkInterface.isLoopback() || networkInterface.isVirtual() || !networkInterface.isUp()) {
                    continue;
                }
                Enumeration addresses = networkInterface.getInetAddresses();
                if (addresses.hasMoreElements()) {
                    return addresses.nextElement();
                }
            }
        } catch (SocketException e) {
        }
        return null;
    }

转载于:https://my.oschina.net/tinglanrmb32/blog/693614

你可能感兴趣的:(linux下使用Java获取本机ip地址?)