這篇文章主要講解了Java怎么實現(xiàn)獲取本地IP地址和主機名,內(nèi)容清晰明了,對此有興趣的小伙伴可以學習一下,相信大家閱讀完之后會有幫助。
青陽ssl適用于網(wǎng)站、小程序/APP、API接口等需要進行數(shù)據(jù)傳輸應用場景,ssl證書未來市場廣闊!成為成都創(chuàng)新互聯(lián)的ssl證書銷售渠道,可以享受市場價格4-6折優(yōu)惠!如果有意向歡迎電話聯(lián)系或者加微信:028-86922220(備注:SSL證書合作)期待與您的合作!
方式一:通過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 { Enumerationfaces = NetworkInterface.getNetworkInterfaces(); while (faces.hasMoreElements()) { // 遍歷網(wǎng)絡接口 NetworkInterface face = faces.nextElement(); if (face.isLoopback() || face.isVirtual() || !face.isUp()) { continue; } System.out.print("網(wǎng)絡接口名:" + face.getDisplayName() + ",地址:"); Enumeration address = face.getInetAddresses(); while (address.hasMoreElements()) { // 遍歷網(wǎng)絡地址 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)絡接口名:VMware Virtual Ethernet Adapter for VMnet8,地址:192.168.153.1
網(wǎng)絡接口名:TAP-Windows Adapter V9,地址:10.8.0.30
網(wǎng)絡接口名:VMware Virtual Ethernet Adapter for VMnet1,地址:192.168.46.1
網(wǎng)絡接口名:Intel(R) Dual Band Wireless-AC 8265,地址:172.16.78.27
疑問?:第一、三行為VM虛擬機網(wǎng)絡地址,不知為何還在。
工具類:
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 { Listlist = new ArrayList<>(); Enumeration enumeration = NetworkInterface.getNetworkInterfaces(); while (enumeration.hasMoreElements()) { NetworkInterface intf = enumeration.nextElement(); if (intf.isLoopback() || intf.isVirtual()) { // continue; } Enumeration 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)容,是不是對Java怎么實現(xiàn)獲取本地IP地址和主機名有進一步的了解,如果還想學習更多內(nèi)容,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。