您好,登錄后才能下訂單哦!
這篇文章主要講解了Java怎么實現獲取本地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地址卻有待考量,如果一臺機器有多個網卡,
他獲取的IP是誰的呢?事實上,上面輸出的IP是我虛擬機IP地址,既不是我有線網卡的地址,也不是我無線網卡的地址。
方式二:利用java.net.NetworkInterface獲取
public void test2() { try { Enumeration<NetworkInterface> faces = NetworkInterface.getNetworkInterfaces(); while (faces.hasMoreElements()) { // 遍歷網絡接口 NetworkInterface face = faces.nextElement(); if (face.isLoopback() || face.isVirtual() || !face.isUp()) { continue; } System.out.print("網絡接口名:" + face.getDisplayName() + ",地址:"); Enumeration<InetAddress> address = face.getInetAddresses(); while (address.hasMoreElements()) { // 遍歷網絡地址 InetAddress addr = address.nextElement(); if (!addr.isLoopbackAddress() && addr.isSiteLocalAddress() && !addr.isAnyLocalAddress()) { System.out.print(addr.getHostAddress() + " "); } } System.out.println(""); } } catch (SocketException e) { e.printStackTrace(); } }
輸出:
網絡接口名:VMware Virtual Ethernet Adapter for VMnet8,地址:192.168.153.1
網絡接口名:TAP-Windows Adapter V9,地址:10.8.0.30
網絡接口名:VMware Virtual Ethernet Adapter for VMnet1,地址:192.168.46.1
網絡接口名:Intel(R) Dual Band Wireless-AC 8265,地址:172.16.78.27
疑問?:第一、三行為VM虛擬機網絡地址,不知為何還在。
工具類:
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(); } /** * 獲取系統首選IP * * @return * @throws UnknownHostException */ public static String getLocalIP() throws UnknownHostException { return InetAddress.getLocalHost().getHostAddress(); } /** * 獲取所有網卡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]); } /** * 判斷操作系統是否是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系統:" + LocalHostUtil.isWindowsOS()); System.out.println("主機名稱:" + LocalHostUtil.getHostName()); System.out.println("系統首選IP:" + LocalHostUtil.getLocalIP()); System.out.println("系統所有IP:" + String.join(",", LocalHostUtil.getLocalIPs())); } catch (UnknownHostException e) { } catch (Exception e) { e.printStackTrace(); } } }
看完上述內容,是不是對Java怎么實現獲取本地IP地址和主機名有進一步的了解,如果還想學習更多內容,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。