首页 > 代码库 > Java获取本机的ip地址

Java获取本机的ip地址

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

 

public static void main(String[] args) throws Exception {        Enumeration<NetworkInterface> netInterfaces = NetworkInterface.getNetworkInterfaces();        while (netInterfaces.hasMoreElements()) {            NetworkInterface ni = netInterfaces.nextElement();            System.out.println("网络接口名称:" + ni.getDisplayName());            Enumeration<InetAddress> ia = ni.getInetAddresses();            while (ia.hasMoreElements()) {                InetAddress ip = ia.nextElement();                System.out.println(ip.getHostAddress());            }        }    }
网络接口名称:Software Loopback Interface 10:0:0:0:0:0:0:1127.0.0.1网络接口名称:WAN Miniport (SSTP)网络接口名称:Microsoft ISATAP Adapter #6fe80:0:0:0:0:5efe:c0a8:bf01%3网络接口名称:Microsoft Kernel Debug Network Adapter网络接口名称:Microsoft ISATAP Adapter #4fe80:0:0:0:0:5efe:c0a8:167%5网络接口名称:WAN Miniport (Network Monitor)网络接口名称:Microsoft ISATAP Adapter #5网络接口名称:WAN Miniport (IPv6)网络接口名称:Microsoft ISATAP Adapter #2fe80:0:0:0:0:5efe:c0a8:b501%9网络接口名称:WAN Miniport (IP)网络接口名称:WAN Miniport (PPPOE)网络接口名称:WAN Miniport (L2TP)网络接口名称:Microsoft Hosted Network Virtual Adapterfe80:0:0:0:b9b1:54f2:26a3:e07f%13192.168.191.1网络接口名称:Microsoft Teredo Tunneling Adapter2001:0:5ef5:79fd:c5b:37fd:3f57:fe98fe80:0:0:0:c5b:37fd:3f57:fe98%14网络接口名称:VMware Virtual Ethernet Adapter for VMnet8fe80:0:0:0:39ac:b0f8:cb0c:2868%15192.168.181.1网络接口名称:Broadcom 802.11 ÍøÂçÊÊÅäÆ÷Ì?ÎÞÏ?ÖÓòÍøfe80:0:0:0:3cb8:6486:b40b:1cb5%16192.168.1.103网络接口名称:VMware Virtual Ethernet Adapter for VMnet1fe80:0:0:0:2dd6:2ca:d599:b57e%17192.168.72.1网络接口名称:Microsoft Wi-Fi Direct Virtual Adapterfe80:0:0:0:4cad:987a:b4a1:f1bb%18网络接口名称:Realtek PCIe FE Family Controllerfe80:0:0:0:dbb:193c:865f:c467%19网络接口名称:WAN Miniport (PPTP)网络接口名称:WAN Miniport (IKEv2)网络接口名称:Microsoft ISATAP Adapter #8fe80: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获取本机的ip地址