Java 詳解如何獲取網(wǎng)絡接口信息
前言
查看本機的網(wǎng)絡接口信息,本文有詳細的介紹哦。
代碼
不廢話,上代碼。
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: 網(wǎng)絡接口 @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獲得網(wǎng)絡設備現(xiàn)實名稱=" + networkInterface.getName());
System.out.println("getDisplayName獲得網(wǎng)絡設備現(xiàn)實名稱=" + networkInterface.getDisplayName());
System.out.println("getIndex獲得網(wǎng)絡接口的索引=" + networkInterface.getIndex());
System.out.println("isUp是否已經(jīng)開啟并運行=" + networkInterface.isUp());
System.out.println("isLoopback是否為回調(diào)接口=" + 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();
}
}
}
看看執(zhí)行結(jié)果。
/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獲得網(wǎng)絡設備現(xiàn)實名稱=utun1
getDisplayName獲得網(wǎng)絡設備現(xiàn)實名稱=utun1
getIndex獲得網(wǎng)絡接口的索引=12
isUp是否已經(jīng)開啟并運行=true
isLoopback是否為回調(diào)接口=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獲得網(wǎng)絡設備現(xiàn)實名稱=utun0
getDisplayName獲得網(wǎng)絡設備現(xiàn)實名稱=utun0
getIndex獲得網(wǎng)絡接口的索引=11
isUp是否已經(jīng)開啟并運行=true
isLoopback是否為回調(diào)接口=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獲得網(wǎng)絡設備現(xiàn)實名稱=llw0
getDisplayName獲得網(wǎng)絡設備現(xiàn)實名稱=llw0
getIndex獲得網(wǎng)絡接口的索引=10
isUp是否已經(jīng)開啟并運行=true
isLoopback是否為回調(diào)接口=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獲得網(wǎng)絡設備現(xiàn)實名稱=awdl0
getDisplayName獲得網(wǎng)絡設備現(xiàn)實名稱=awdl0
getIndex獲得網(wǎng)絡接口的索引=9
isUp是否已經(jīng)開啟并運行=true
isLoopback是否為回調(diào)接口=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獲得網(wǎng)絡設備現(xiàn)實名稱=en0
getDisplayName獲得網(wǎng)絡設備現(xiàn)實名稱=en0
getIndex獲得網(wǎng)絡接口的索引=4
isUp是否已經(jīng)開啟并運行=true
isLoopback是否為回調(diào)接口=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獲得網(wǎng)絡設備現(xiàn)實名稱=lo0
getDisplayName獲得網(wǎng)絡設備現(xiàn)實名稱=lo0
getIndex獲得網(wǎng)絡接口的索引=1
isUp是否已經(jīng)開啟并運行=true
isLoopback是否為回調(diào)接口=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
總結(jié)
認定一件事,即使拿出十分力氣都無法完成,也要拿出十二分力氣去努力。
如果本文對你有用的話,別吝嗇你的贊,Come on。

到此這篇關(guān)于Java 詳解如何獲取網(wǎng)絡接口信息的文章就介紹到這了,更多相關(guān)Java 獲取網(wǎng)絡接口內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
教你安裝eclipse2021并配置內(nèi)網(wǎng)maven中心倉庫的圖文詳解
本文能通過圖文并茂的形式給大家介紹安裝eclipse2021并配置內(nèi)網(wǎng)maven中心倉庫的相關(guān)知識,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友參考下吧2021-09-09
Springboot+WebSocket+Netty實現(xiàn)在線聊天/群聊系統(tǒng)
這篇文章主要實現(xiàn)在好友添加、建群、聊天對話、群聊功能,使用Java作為后端語言進行支持,界面友好,開發(fā)簡單,文章中有詳細的代碼示例供大家參考,需要的朋友可以參考下2023-08-08
java實現(xiàn)連接mysql數(shù)據(jù)庫單元測試查詢數(shù)據(jù)的實例代碼
下面小編就為大家?guī)硪黄猨ava實現(xiàn)連接mysql數(shù)據(jù)庫單元測試查詢數(shù)據(jù)的實例代碼。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-10-10

