查找本地计算机IP地址,不会出现误报

时间:2017-12-02 12:26:14

标签: java networking ip ipv4

使用Java我试图找到我的PC本地IP地址。但是,结果并不正确。具体来说,我需要<img width=200 id='img' src='https://s.aolcdn.com/hss/storage/midas/b386937631a1f03665c1d57289070898/203417456/simpsons.jpg'>命令中的无线LAN适配器WiFi IPv4地址。我查看了许多不同的问题,例如this excellent answer,但是在Java中如何做到这一点并不清楚。 ipconfig返回错误的IP('Ethernet-Adapter VirtualBox Host-Only Network')。

我尝试了以下代码:

InetAddress.getLocalHost()

注意:
public static List<String> getLocalIPAddresses() throws Exception { val networkInterfaces = NetworkInterface.getNetworkInterfaces(); val localIPAddresses = new ArrayList<String>(); while (networkInterfaces.hasMoreElements()) { for (val interfaceAddress : networkInterfaces.nextElement().getInterfaceAddresses()) { val address = interfaceAddress.getAddress(); // Find the local IP addresses if (address.isSiteLocalAddress()) { val localIPAddress = interfaceAddress.getAddress().toString().replace("/", ""); localIPAddresses.add(localIPAddress); } } } if (localIPAddresses.isEmpty()) { throw new IllegalStateException("Expected the computer's local IP address but didn't get one!"); } return localIPAddresses; } 来自Lombok

在我的机器上,它返回一个包含2个IP地址的列表:

val

'Ethernet-Adapter VirtualBox Host-Only Network'one和正确的'无线LAN适配器WiFi'之一。我该怎样做才能始终保证后者?显然检查192.168.56.1 192.168.2.103 (a.k.a本地IP地址)是不够的。还有什么可以按代码完成的?过滤掉'VirtualBox'并不是一个万无一失的解决方案,特别是因为网络接口名称依赖于语言:

isSiteLocalAddress()

我需要一个独立于平台的解决方案。我虽然在Windows上。

1 个答案:

答案 0 :(得分:0)

为什么不使用isVirtual()方法。

static List<String> getLocalIpAddress() throws SocketException {
    Enumeration<NetworkInterface> networkInterfaces = null;
    List<String> localIpAddress = new ArrayList<String>();

    networkInterfaces = NetworkInterface.getNetworkInterfaces();

    while (networkInterfaces.hasMoreElements()) {
        NetworkInterface ni = networkInterfaces.nextElement();
        System.out.println("=== <" + ni.getDisplayName() + "> ===");

        if (!ni.isUp()) {
            for (InterfaceAddress addr : ni.getInterfaceAddresses()) {
                System.out.println("not up : " + addr.getAddress().getHostAddress());
            }
            continue;
        }
        if (ni.isLoopback()) {
            for (InterfaceAddress addr : ni.getInterfaceAddresses()) {
                System.out.println("loopback : " + addr.getAddress().getHostAddress());
            }
            continue;
        }
        if (ni.isVirtual()) {
            for (InterfaceAddress addr : ni.getInterfaceAddresses()) {
                System.out.println("virtual : " + addr.getAddress().getHostAddress());
            }
            continue;
        }

        if (!ni.isPointToPoint()) {
            for (InterfaceAddress addr : ni.getInterfaceAddresses()) {
                System.out.println("not ppp : " + addr.getAddress().getHostAddress());
            }
            continue;
        }

        for (InterfaceAddress addr : ni.getInterfaceAddresses()) {
            InetAddress address = addr.getAddress();
            if (address.isLinkLocalAddress()) {
                System.out.println("link local address: " + address.getHostName() + address.getHostAddress());
                continue;
            }
            if (address.isSiteLocalAddress()) {
                System.out.println("site local address: " + address.getHostName() + address.getHostAddress());
                continue;
            }

            System.out.println("getHostName: " + address.getHostName());
            System.out.println("getHostAddress : " + address.getHostAddress());
            System.out.println("------separator-------");

            localIpAddress.add(address.getHostAddress());
        }
    }

    return localIpAddress;
}