3

如何在 windows 中获取网络适配器的媒体状态?我搜索了一下,它看起来不像 java.net.NetworkInterface 类提供了这种能力,这是有道理的,因为它似乎是一个 Windows 概念,甚至不适用于所有适配器。当我运行 ipconfig 时,我得到如下内容:

Windows IP Configuration


Wireless LAN adapter Wireless Network Connection 5:

   Connection-specific DNS Suffix  . : BlahBlah.Blah
   IPv4 Address. . . . . . . . . . . : 192.168.113.44
   Subnet Mask . . . . . . . . . . . : 255.255.255.0
   Default Gateway . . . . . . . . . : 192.168.113.1

Ethernet adapter Local Area Connection 8:

   Media State . . . . . . . . . . . : Media disconnected
   Connection-specific DNS Suffix  . : BlahBlah.Blah

而且,如果我使用在任何人都可以解释为什么 Java GetNetworkInterfaces 在 Windows 7 上返回这么多接口以从这些适配器获取详细信息(删除其他适配器)中找到的脚本,我会得到如下内容:

Display name: Software Loopback Interface 1
Name: lo
InetAddress: /0:0:0:0:0:0:0:1
InetAddress: /127.0.0.1
Up? true
Loopback? true
PointToPoint? false
Supports multicast? true
Virtual? false
Hardware address: []
MTU: -1

Display name: Intel(R) 82579LM Gigabit Network Connection #2
Name: eth13
InetAddress: /172.16.4.29
Up? false
Loopback? false
PointToPoint? false
Supports multicast? true
Virtual? false
Hardware address: [60, -105, 14, -77, -123, 110]
MTU: 1500

Display name: Intel(R) Centrino(R) Advanced-N 6205 #3
Name: net11
InetAddress: /192.168.113.44
Up? false
Loopback? false
PointToPoint? false
Supports multicast? true
Virtual? false
Hardware address: [60, -105, 14, -77, -123, 110]
MTU: 1500

关于如何确定以太网适配器是否“媒体已断开”的想法?

4

1 回答 1

1

您可以参考以下代码段。基本上!networkInterface.isUp()应该为您解决问题。

List<NetworkInterface> nic = new ArrayList<>();
interfaces = NetworkInterface.getNetworkInterfaces();
while (interfaces.hasMoreElements()) {
    NetworkInterface networkInterface = interfaces.nextElement();
    if (!networkInterface.isUp())
        continue;
    nic.add(networkinterface);
}
于 2017-04-05T19:07:37.783 回答