基于Java實現(xiàn)獲取本地IP地址和主機名
方式一:通過java.net.InetAddress類獲取
public void test1() { try { InetAddress addr = InetAddress.getLocalHost(); System.out.println("IP地址:" + addr.getHostAddress() + ",主機名:" + addr.getHostName()); } catch (UnknownHostException e) { e.printStackTrace(); } }
輸出:
IP地址:192.168.153.1,主機名:DESKTOP-338UP3E
這種方式獲取到的主機名沒啥問題,這種方式獲取的主機名沒啥問題,但獲取到的IP地址卻有待考量,如果一臺機器有多個網(wǎng)卡,
他獲取的IP是誰的呢?事實上,上面輸出的IP是我虛擬機IP地址,既不是我有線網(wǎng)卡的地址,也不是我無線網(wǎng)卡的地址。
方式二:利用java.net.NetworkInterface獲取
public void test2() { try { Enumeration<NetworkInterface> faces = NetworkInterface.getNetworkInterfaces(); while (faces.hasMoreElements()) { // 遍歷網(wǎng)絡(luò)接口 NetworkInterface face = faces.nextElement(); if (face.isLoopback() || face.isVirtual() || !face.isUp()) { continue; } System.out.print("網(wǎng)絡(luò)接口名:" + face.getDisplayName() + ",地址:"); Enumeration<InetAddress> address = face.getInetAddresses(); while (address.hasMoreElements()) { // 遍歷網(wǎng)絡(luò)地址 InetAddress addr = address.nextElement(); if (!addr.isLoopbackAddress() && addr.isSiteLocalAddress() && !addr.isAnyLocalAddress()) { System.out.print(addr.getHostAddress() + " "); } } System.out.println(""); } } catch (SocketException e) { e.printStackTrace(); } }
輸出:
網(wǎng)絡(luò)接口名:VMware Virtual Ethernet Adapter for VMnet8,地址:192.168.153.1
網(wǎng)絡(luò)接口名:TAP-Windows Adapter V9,地址:10.8.0.30
網(wǎng)絡(luò)接口名:VMware Virtual Ethernet Adapter for VMnet1,地址:192.168.46.1
網(wǎng)絡(luò)接口名:Intel(R) Dual Band Wireless-AC 8265,地址:172.16.78.27
疑問?:第一、三行為VM虛擬機網(wǎng)絡(luò)地址,不知為何還在。
工具類:
import java.net.InetAddress; import java.net.NetworkInterface; import java.net.SocketException; import java.net.UnknownHostException; import java.util.ArrayList; import java.util.Enumeration; import java.util.List; /** * 本地主機工具類 * * @author zhi * @since 2019年11月13日09:04:36 * */ public class LocalHostUtil { /** * 獲取主機名稱 * * @return * @throws UnknownHostException */ public static String getHostName() throws UnknownHostException { return InetAddress.getLocalHost().getHostName(); } /** * 獲取系統(tǒng)首選IP * * @return * @throws UnknownHostException */ public static String getLocalIP() throws UnknownHostException { return InetAddress.getLocalHost().getHostAddress(); } /** * 獲取所有網(wǎng)卡IP,排除回文地址、虛擬地址 * * @return * @throws SocketException */ public static String[] getLocalIPs() throws SocketException { List<String> list = new ArrayList<>(); Enumeration<NetworkInterface> enumeration = NetworkInterface.getNetworkInterfaces(); while (enumeration.hasMoreElements()) { NetworkInterface intf = enumeration.nextElement(); if (intf.isLoopback() || intf.isVirtual()) { // continue; } Enumeration<InetAddress> inets = intf.getInetAddresses(); while (inets.hasMoreElements()) { InetAddress addr = inets.nextElement(); if (addr.isLoopbackAddress() || !addr.isSiteLocalAddress() || addr.isAnyLocalAddress()) { continue; } list.add(addr.getHostAddress()); } } return list.toArray(new String[0]); } /** * 判斷操作系統(tǒng)是否是Windows * * @return */ public static boolean isWindowsOS() { boolean isWindowsOS = false; String osName = System.getProperty("os.name"); if (osName.toLowerCase().indexOf("windows") > -1) { isWindowsOS = true; } return isWindowsOS; } public static void main(String[] args) { try { System.out.println("主機是否為Windows系統(tǒng):" + LocalHostUtil.isWindowsOS()); System.out.println("主機名稱:" + LocalHostUtil.getHostName()); System.out.println("系統(tǒng)首選IP:" + LocalHostUtil.getLocalIP()); System.out.println("系統(tǒng)所有IP:" + String.join(",", LocalHostUtil.getLocalIPs())); } catch (UnknownHostException e) { } catch (Exception e) { e.printStackTrace(); } } }
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Java實現(xiàn)ip地址和int數(shù)字的相互轉(zhuǎn)換
- Java判斷字符串是否為IP地址的方法
- java如何獲取本機IP地址
- java IP地址網(wǎng)段計算的示例代碼
- Java實現(xiàn)復(fù)原IP地址的方法
- java 偽造http請求ip地址的方法
- java獲取ip地址與網(wǎng)絡(luò)接口的方法示例
- Java 根據(jù)網(wǎng)址查詢DNS/IP地址的方法
- Java利用Request請求如何獲取IP地址對應(yīng)的省份、城市詳解
- Java利用Request請求獲取IP地址的方法詳解
- JAVA如何獲取客戶端IP地址和MAC地址
- Java獲取電腦真實IP地址的示例代碼
相關(guān)文章
Mybatis-plus selectByMap條件查詢方式
這篇文章主要介紹了Mybatis-plus selectByMap條件查詢方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-06-06java編程創(chuàng)建型設(shè)計模式工廠方法模式示例詳解
這篇文章主要為大家介紹了java編程創(chuàng)建型設(shè)計模式之工廠方法模式的創(chuàng)建及案例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助2022-02-02聊聊在獲取方法參數(shù)名方面,Spring真的就比Mybatis強?
在獲取方法參數(shù)名方面,Spring真的就比Mybatis強嗎?今天就帶大家聊聊這個話題,如有錯誤或未考慮完全的地方,望不吝賜教2021-12-12Java實現(xiàn)AES/CBC/PKCS7Padding加解密的方法
這篇文章主要介紹了Java實現(xiàn)AES/CBC/PKCS7Padding加解密的方法,本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-08-08