Java获取本机的ip地址

说到获取ip地址,有人可能会想到,直接用InetAddress.getLocalHost().getHostAddress()。实际上这个是不对的,因为一台机器上可能有多个网络接口(一般指网卡或者虚拟网卡),因此也就有多个ip地址,所以我们需要列出所有的网络接口及其对应的ip地址。代码如下:

public static void main(String[] args) throws Exception {
        Enumeration netInterfaces = NetworkInterface.getNetworkInterfaces();
        while (netInterfaces.hasMoreElements()) {
            NetworkInterface ni = netInterfaces.nextElement();
            System.out.println("网络接口名称:" + ni.getDisplayName());
            Enumeration ia = ni.getInetAddresses();
            while (ia.hasMoreElements()) {
                InetAddress ip = ia.nextElement();
                System.out.println(ip.getHostAddress());
            }
        }
    }
网络接口名称:Software Loopback Interface 1
0:0:0:0:0:0:0:1
127.0.0.1
网络接口名称:WAN Miniport (SSTP)
网络接口名称:Microsoft ISATAP Adapter #6
fe80:0:0:0:0:5efe:c0a8:bf01%3
网络接口名称:Microsoft Kernel Debug Network Adapter
网络接口名称:Microsoft ISATAP Adapter #4
fe80:0:0:0:0:5efe:c0a8:167%5
网络接口名称:WAN Miniport (Network Monitor)
网络接口名称:Microsoft ISATAP Adapter #5
网络接口名称:WAN Miniport (IPv6)
网络接口名称:Microsoft ISATAP Adapter #2
fe80:0:0:0:0:5efe:c0a8:b501%9
网络接口名称:WAN Miniport (IP)
网络接口名称:WAN Miniport (PPPOE)
网络接口名称:WAN Miniport (L2TP)
网络接口名称:Microsoft Hosted Network Virtual Adapter
fe80:0:0:0:b9b1:54f2:26a3:e07f%13
192.168.191.1
网络接口名称:Microsoft Teredo Tunneling Adapter
2001:0:5ef5:79fd:c5b:37fd:3f57:fe98
fe80:0:0:0:c5b:37fd:3f57:fe98%14
网络接口名称:VMware Virtual Ethernet Adapter for VMnet8
fe80:0:0:0:39ac:b0f8:cb0c:2868%15
192.168.181.1
网络接口名称:Broadcom 802.11 ÍøÂçÊÊÅäÆ÷ÌṩÎÞÏ߾ÖÓòÍø
fe80:0:0:0:3cb8:6486:b40b:1cb5%16
192.168.1.103
网络接口名称:VMware Virtual Ethernet Adapter for VMnet1
fe80:0:0:0:2dd6:2ca:d599:b57e%17
192.168.72.1
网络接口名称:Microsoft Wi-Fi Direct Virtual Adapter
fe80:0:0:0:4cad:987a:b4a1:f1bb%18
网络接口名称:Realtek PCIe FE Family Controller
fe80:0:0:0:dbb:193c:865f:c467%19
网络接口名称:WAN Miniport (PPTP)
网络接口名称:WAN Miniport (IKEv2)
网络接口名称:Microsoft ISATAP Adapter #8
fe80:0:0:0:0:5efe:c0a8:4801%22
网络接口名称:Remote NDIS based Internet Sharing Device
网络接口名称:Realtek PCIe FE Family Controller-WFP Native MAC Layer LightWeight Filter-0000
网络接口名称:Realtek PCIe FE Family Controller-Liebao Wifi NAT Driver-0000
网络接口名称:Realtek PCIe FE Family Controller-QoS Packet Scheduler-0000
网络接口名称:Realtek PCIe FE Family Controller-WFP 802.3 MAC Layer LightWeight Filter-0000
网络接口名称:Broadcom 802.11 ÍøÂçÊÊÅäÆ÷ÌṩÎÞÏ߾ÖÓòÍø¡£-WFP Native MAC Layer LightWeight Filter-00
网络接口名称:Broadcom 802.11 ÍøÂçÊÊÅäÆ÷ÌṩÎÞÏ߾ÖÓòÍø¡£-Virtual WiFi Filter Driver-00
网络接口名称:Broadcom 802.11 ÍøÂçÊÊÅäÆ÷ÌṩÎÞÏ߾ÖÓòÍø¡£-Native WiFi Filter Driver-00
网络接口名称:Broadcom 802.11 ÍøÂçÊÊÅäÆ÷ÌṩÎÞÏ߾ÖÓòÍø¡£-Liebao Wifi NAT Driver-00
网络接口名称:Broadcom 802.11 ÍøÂçÊÊÅäÆ÷ÌṩÎÞÏ߾ÖÓòÍø¡£-QoS Packet Scheduler-00
网络接口名称:Broadcom 802.11 ÍøÂçÊÊÅäÆ÷ÌṩÎÞÏ߾ÖÓòÍø¡£-WFP 802.3 MAC Layer LightWeight Filter-00
网络接口名称:Microsoft Wi-Fi Direct Virtual Adapter-WFP Native MAC Layer LightWeight Filter-0000
网络接口名称:Microsoft Hosted Network Virtual Adapter-WFP Native MAC Layer LightWeight Filter-0000
网络接口名称:Microsoft Hosted Network Virtual Adapter-Native WiFi Filter Driver-0000
网络接口名称:Microsoft Wi-Fi Direct Virtual Adapter-Native WiFi Filter Driver-0000
网络接口名称:Microsoft Wi-Fi Direct Virtual Adapter-Liebao Wifi NAT Driver-0000
网络接口名称:Microsoft Wi-Fi Direct Virtual Adapter-QoS Packet Scheduler-0000
网络接口名称:Microsoft Wi-Fi Direct Virtual Adapter-WFP 802.3 MAC Layer LightWeight Filter-0000
网络接口名称:Microsoft Hosted Network Virtual Adapter-Liebao Wifi NAT Driver-0000
网络接口名称:Microsoft Hosted Network Virtual Adapter-QoS Packet Scheduler-0000
网络接口名称:Microsoft Hosted Network Virtual Adapter-WFP 802.3 MAC Layer LightWeight Filter-0000
网络接口名称:WAN Miniport (IP)-WFP Native MAC Layer LightWeight Filter-0000
网络接口名称:WAN Miniport (IP)-Liebao Wifi NAT Driver-0000
网络接口名称:WAN Miniport (IP)-QoS Packet Scheduler-0000
网络接口名称:WAN Miniport (IPv6)-WFP Native MAC Layer LightWeight Filter-0000
网络接口名称:WAN Miniport (IPv6)-Liebao Wifi NAT Driver-0000
网络接口名称:WAN Miniport (IPv6)-QoS Packet Scheduler-0000
网络接口名称:WAN Miniport (Network Monitor)-WFP Native MAC Layer LightWeight Filter-0000
网络接口名称:WAN Miniport (Network Monitor)-Liebao Wifi NAT Driver-0000
网络接口名称:WAN Miniport (Network Monitor)-QoS Packet Scheduler-0000

其中像fe80:0:0:0:0:5efe:c0a8:bf01%3这种是IPV6格式的地址,共128位,每16位用分号分开,最后的3%表示这个表示该地址仅限于标号为3的网络接口,而在其他网络接口这个地址是无效的。

你可能感兴趣的:(Java,SE)