前言
查看本机的网络接口信息,本文有详细的介绍哦。
代码
不废话,上代码。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
|
package com.hy.csdn.tools; import java.net.InetAddress; import java.net.NetworkInterface; import java.net.SocketException; import java.util.Enumeration; /** * @Program: hy-utils @ClassName: StuNetworkInterface @Author: huyi @Date: 2020-09-20 * 23:48 @Description: 网络接口 @Version: V1.0 */ public class NetworkInterfaceUtil { public static void main(String[] args) { try { Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces(); while (networkInterfaces.hasMoreElements()) { NetworkInterface networkInterface = networkInterfaces.nextElement(); System.out.println( "getName获得网络设备现实名称=" + networkInterface.getName()); System.out.println( "getDisplayName获得网络设备现实名称=" + networkInterface.getDisplayName()); System.out.println( "getIndex获得网络接口的索引=" + networkInterface.getIndex()); System.out.println( "isUp是否已经开启并运行=" + networkInterface.isUp()); System.out.println( "isLoopback是否为回调接口=" + networkInterface.isLoopback()); System.out.println( "getMTU获得最大传输单元=" + networkInterface.getMTU()); Enumeration<InetAddress> enumerations = networkInterface.getInetAddresses(); while (enumerations.hasMoreElements()) { InetAddress inetAddress = enumerations.nextElement(); System.out.println( "获取此IP地址的完全限定域名=" + inetAddress.getCanonicalHostName()); System.out.println( "获取此IP地址的主机名=" + inetAddress.getHostName()); System.out.println( "获取此IP地址的字符串=" + inetAddress.getHostAddress()); System.out.print( "getAddress返回此InetAddress对象的原始IP地址=" ); byte [] addressByte = inetAddress.getAddress(); for ( byte b : addressByte) { System.out.print(b + " " ); } System.out.println(); } System.out.println(); } } catch (SocketException e) { e.printStackTrace(); } } } |
看看执行结果。
/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/bin/java -Dfile.encoding=UTF-8 -classpath /Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/jre/lib/charsets.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/jre/lib/deploy.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/jre/lib/ext/cldrdata.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/jre/lib/ext/dnsns.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/jre/lib/ext/jaccess.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/jre/lib/ext/jfxrt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/jre/lib/ext/localedata.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/jre/lib/ext/nashorn.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/jre/lib/ext/sunec.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/jre/lib/ext/sunjce_provider.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/jre/lib/ext/sunpkcs11.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/jre/lib/ext/zipfs.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/jre/lib/javaws.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/jre/lib/jce.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/jre/lib/jfr.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/jre/lib/jfxswt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/jre/lib/jsse.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/jre/lib/management-agent.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/jre/lib/plugin.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/jre/lib/resources.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/jre/lib/rt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/lib/ant-javafx.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/lib/dt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/lib/javafx-mx.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/lib/jconsole.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/lib/packager.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/lib/sa-jdi.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/lib/tools.jar:/Users/huyi/idea/csdn/target/classes:/Users/huyi/.m2/repository/org/springframework/boot/spring-boot-starter-web/2.5.5/spring-boot-starter-web-2.5.5.jar:/Users/huyi/.m2/repository/org/springframework/boot/spring-boot-starter/2.5.5/spring-boot-starter-2.5.5.jar:/Users/huyi/.m2/repository/org/springframework/boot/spring-boot-starter-logging/2.5.5/spring-boot-starter-logging-2.5.5.jar:/Users/huyi/.m2/repository/ch/qos/logback/logback-classic/1.2.6/logback-classic-1.2.6.jar:/Users/huyi/.m2/repository/ch/qos/logback/logback-core/1.2.6/logback-core-1.2.6.jar:/Users/huyi/.m2/repository/org/apache/logging/log4j/log4j-to-slf4j/2.14.1/log4j-to-slf4j-2.14.1.jar:/Users/huyi/.m2/repository/org/apache/logging/log4j/log4j-api/2.14.1/log4j-api-2.14.1.jar:/Users/huyi/.m2/repository/org/slf4j/jul-to-slf4j/1.7.32/jul-to-slf4j-1.7.32.jar:/Users/huyi/.m2/repository/jakarta/annotation/jakarta.annotation-api/1.3.5/jakarta.annotation-api-1.3.5.jar:/Users/huyi/.m2/repository/org/yaml/snakeyaml/1.28/snakeyaml-1.28.jar:/Users/huyi/.m2/repository/org/springframework/boot/spring-boot-starter-json/2.5.5/spring-boot-starter-json-2.5.5.jar:/Users/huyi/.m2/repository/com/fasterxml/jackson/core/jackson-databind/2.12.5/jackson-databind-2.12.5.jar:/Users/huyi/.m2/repository/com/fasterxml/jackson/core/jackson-annotations/2.12.5/jackson-annotations-2.12.5.jar:/Users/huyi/.m2/repository/com/fasterxml/jackson/core/jackson-core/2.12.5/jackson-core-2.12.5.jar:/Users/huyi/.m2/repository/com/fasterxml/jackson/datatype/jackson-datatype-jdk8/2.12.5/jackson-datatype-jdk8-2.12.5.jar:/Users/huyi/.m2/repository/com/fasterxml/jackson/datatype/jackson-datatype-jsr310/2.12.5/jackson-datatype-jsr310-2.12.5.jar:/Users/huyi/.m2/repository/com/fasterxml/jackson/module/jackson-module-parameter-names/2.12.5/jackson-module-parameter-names-2.12.5.jar:/Users/huyi/.m2/repository/org/springframework/boot/spring-boot-starter-tomcat/2.5.5/spring-boot-starter-tomcat-2.5.5.jar:/Users/huyi/.m2/repository/org/apache/tomcat/embed/tomcat-embed-core/9.0.53/tomcat-embed-core-9.0.53.jar:/Users/huyi/.m2/repository/org/apache/tomcat/embed/tomcat-embed-el/9.0.53/tomcat-embed-el-9.0.53.jar:/Users/huyi/.m2/repository/org/apache/tomcat/embed/tomcat-embed-websocket/9.0.53/tomcat-embed-websocket-9.0.53.jar:/Users/huyi/.m2/repository/org/springframework/spring-web/5.3.10/spring-web-5.3.10.jar:/Users/huyi/.m2/repository/org/springframework/spring-beans/5.3.10/spring-beans-5.3.10.jar:/Users/huyi/.m2/repository/org/springframework/spring-webmvc/5.3.10/spring-webmvc-5.3.10.jar:/Users/huyi/.m2/repository/org/springframework/spring-aop/5.3.10/spring-aop-5.3.10.jar:/Users/huyi/.m2/repository/org/springframework/spring-context/5.3.10/spring-context-5.3.10.jar:/Users/huyi/.m2/repository/org/springframework/spring-expression/5.3.10/spring-expression-5.3.10.jar:/Users/huyi/.m2/repository/org/springframework/boot/spring-boot-devtools/2.5.5/spring-boot-devtools-2.5.5.jar:/Users/huyi/.m2/repository/org/springframework/boot/spring-boot/2.5.5/spring-boot-2.5.5.jar:/Users/huyi/.m2/repository/org/springframework/boot/spring-boot-autoconfigure/2.5.5/spring-boot-autoconfigure-2.5.5.jar:/Users/huyi/.m2/repository/org/springframework/boot/spring-boot-configuration-processor/2.5.5/spring-boot-configuration-processor-2.5.5.jar:/Users/huyi/.m2/repository/org/projectlombok/lombok/1.18.20/lombok-1.18.20.jar:/Users/huyi/.m2/repository/org/slf4j/slf4j-api/1.7.32/slf4j-api-1.7.32.jar:/Users/huyi/.m2/repository/org/springframework/spring-core/5.3.10/spring-core-5.3.10.jar:/Users/huyi/.m2/repository/org/springframework/spring-jcl/5.3.10/spring-jcl-5.3.10.jar:/Users/huyi/.m2/repository/com/aliyun/oss/aliyun-sdk-oss/3.10.2/aliyun-sdk-oss-3.10.2.jar:/Users/huyi/.m2/repository/org/apache/httpcomponents/httpclient/4.5.13/httpclient-4.5.13.jar:/Users/huyi/.m2/repository/org/apache/httpcomponents/httpcore/4.4.14/httpcore-4.4.14.jar:/Users/huyi/.m2/repository/commons-codec/commons-codec/1.15/commons-codec-1.15.jar:/Users/huyi/.m2/repository/org/jdom/jdom2/2.0.6/jdom2-2.0.6.jar:/Users/huyi/.m2/repository/org/codehaus/jettison/jettison/1.1/jettison-1.1.jar:/Users/huyi/.m2/repository/stax/stax-api/1.0.1/stax-api-1.0.1.jar:/Users/huyi/.m2/repository/com/aliyun/aliyun-java-sdk-core/3.4.0/aliyun-java-sdk-core-3.4.0.jar:/Users/huyi/.m2/repository/com/aliyun/aliyun-java-sdk-ram/3.0.0/aliyun-java-sdk-ram-3.0.0.jar:/Users/huyi/.m2/repository/com/aliyun/aliyun-java-sdk-sts/3.0.0/aliyun-java-sdk-sts-3.0.0.jar:/Users/huyi/.m2/repository/com/aliyun/aliyun-java-sdk-ecs/4.2.0/aliyun-java-sdk-ecs-4.2.0.jar:/Users/huyi/.m2/repository/com/aliyun/aliyun-java-sdk-kms/2.7.0/aliyun-java-sdk-kms-2.7.0.jar:/Users/huyi/.m2/repository/com/google/code/gson/gson/2.8.8/gson-2.8.8.jar:/Users/huyi/.m2/repository/cn/hutool/hutool-all/5.7.15/hutool-all-5.7.15.jar com.hy.csdn.tools.NetworkInterfaceUtil
getName获得网络设备现实名称=utun1
getDisplayName获得网络设备现实名称=utun1
getIndex获得网络接口的索引=12
isUp是否已经开启并运行=true
isLoopback是否为回调接口=false
getMTU获得最大传输单元=2000
获取此IP地址的完全限定域名=fe80:0:0:0:63a3:85eb:e7ec:770d%utun1
获取此IP地址的主机名=fe80:0:0:0:63a3:85eb:e7ec:770d%utun1
获取此IP地址的字符串=fe80:0:0:0:63a3:85eb:e7ec:770d%utun1
getAddress返回此InetAddress对象的原始IP地址=-2 -128 0 0 0 0 0 0 99 -93 -123 -21 -25 -20 119 13
getName获得网络设备现实名称=utun0
getDisplayName获得网络设备现实名称=utun0
getIndex获得网络接口的索引=11
isUp是否已经开启并运行=true
isLoopback是否为回调接口=false
getMTU获得最大传输单元=1380
获取此IP地址的完全限定域名=fe80:0:0:0:3b2:454d:40f6:fa91%utun0
获取此IP地址的主机名=fe80:0:0:0:3b2:454d:40f6:fa91%utun0
获取此IP地址的字符串=fe80:0:0:0:3b2:454d:40f6:fa91%utun0
getAddress返回此InetAddress对象的原始IP地址=-2 -128 0 0 0 0 0 0 3 -78 69 77 64 -10 -6 -111
getName获得网络设备现实名称=llw0
getDisplayName获得网络设备现实名称=llw0
getIndex获得网络接口的索引=10
isUp是否已经开启并运行=true
isLoopback是否为回调接口=false
getMTU获得最大传输单元=1500
获取此IP地址的完全限定域名=fe80:0:0:0:1c5b:adff:fec2:41ef%llw0
获取此IP地址的主机名=fe80:0:0:0:1c5b:adff:fec2:41ef%llw0
获取此IP地址的字符串=fe80:0:0:0:1c5b:adff:fec2:41ef%llw0
getAddress返回此InetAddress对象的原始IP地址=-2 -128 0 0 0 0 0 0 28 91 -83 -1 -2 -62 65 -17
getName获得网络设备现实名称=awdl0
getDisplayName获得网络设备现实名称=awdl0
getIndex获得网络接口的索引=9
isUp是否已经开启并运行=true
isLoopback是否为回调接口=false
getMTU获得最大传输单元=1484
获取此IP地址的完全限定域名=fe80:0:0:0:1c5b:adff:fec2:41ef%awdl0
获取此IP地址的主机名=fe80:0:0:0:1c5b:adff:fec2:41ef%awdl0
获取此IP地址的字符串=fe80:0:0:0:1c5b:adff:fec2:41ef%awdl0
getAddress返回此InetAddress对象的原始IP地址=-2 -128 0 0 0 0 0 0 28 91 -83 -1 -2 -62 65 -17
getName获得网络设备现实名称=en0
getDisplayName获得网络设备现实名称=en0
getIndex获得网络接口的索引=4
isUp是否已经开启并运行=true
isLoopback是否为回调接口=false
getMTU获得最大传输单元=1500
获取此IP地址的完全限定域名=fe80:0:0:0:df:d7c9:cd1:81e3%en0
获取此IP地址的主机名=fe80:0:0:0:df:d7c9:cd1:81e3%en0
获取此IP地址的字符串=fe80:0:0:0:df:d7c9:cd1:81e3%en0
getAddress返回此InetAddress对象的原始IP地址=-2 -128 0 0 0 0 0 0 0 -33 -41 -55 12 -47 -127 -29
获取此IP地址的完全限定域名=192.168.3.107
获取此IP地址的主机名=192.168.3.107
获取此IP地址的字符串=192.168.3.107
getAddress返回此InetAddress对象的原始IP地址=-64 -88 3 107
getName获得网络设备现实名称=lo0
getDisplayName获得网络设备现实名称=lo0
getIndex获得网络接口的索引=1
isUp是否已经开启并运行=true
isLoopback是否为回调接口=true
getMTU获得最大传输单元=16384
获取此IP地址的完全限定域名=fe80:0:0:0:0:0:0:1%lo0
获取此IP地址的主机名=fe80:0:0:0:0:0:0:1%lo0
获取此IP地址的字符串=fe80:0:0:0:0:0:0:1%lo0
getAddress返回此InetAddress对象的原始IP地址=-2 -128 0 0 0 0 0 0 0 0 0 0 0 0 0 1
获取此IP地址的完全限定域名=localhost
获取此IP地址的主机名=localhost
获取此IP地址的字符串=0:0:0:0:0:0:0:1
getAddress返回此InetAddress对象的原始IP地址=0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
获取此IP地址的完全限定域名=localhost
获取此IP地址的主机名=localhost
获取此IP地址的字符串=127.0.0.1
getAddress返回此InetAddress对象的原始IP地址=127 0 0 1
Process finished with exit code 0
总结
认定一件事,即使拿出十分力气都无法完成,也要拿出十二分力气去努力。
如果本文对你有用的话,别吝啬你的赞,Come on。
到此这篇关于Java 详解如何获取网络接口信息的文章就介绍到这了,更多相关Java 获取网络接口内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!
原文链接:https://huyi-aliang.blog.csdn.net/article/details/121090654