基于Java實(shí)現(xiàn)獲取本地IP地址和主機(jī)名
方式一:通過(guò)java.net.InetAddress類獲取
public void test1() { try { InetAddress addr = InetAddress.getLocalHost(); System.out.println('IP地址:' + addr.getHostAddress() + ',主機(jī)名:' + addr.getHostName()); } catch (UnknownHostException e) { e.printStackTrace(); }}
輸出:
IP地址:192.168.153.1,主機(jī)名:DESKTOP-338UP3E
這種方式獲取到的主機(jī)名沒(méi)啥問(wèn)題,這種方式獲取的主機(jī)名沒(méi)啥問(wèn)題,但獲取到的IP地址卻有待考量,如果一臺(tái)機(jī)器有多個(gè)網(wǎng)卡,
他獲取的IP是誰(shuí)的呢?事實(shí)上,上面輸出的IP是我虛擬機(jī)IP地址,既不是我有線網(wǎng)卡的地址,也不是我無(wú)線網(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
疑問(wèn)?:第一、三行為VM虛擬機(jī)網(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;/** * 本地主機(jī)工具類 * * @author zhi * @since 2019年11月13日09:04:36 * */public class LocalHostUtil { /** * 獲取主機(jī)名稱 * * @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('主機(jī)是否為Windows系統(tǒng):' + LocalHostUtil.isWindowsOS()); System.out.println('主機(jī)名稱:' + 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(); } }}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. asp(vbs)Rs.Open和Conn.Execute的詳解和區(qū)別及&H0001的說(shuō)明2. CSS hack用法案例詳解3. ASP 處理JSON數(shù)據(jù)的實(shí)現(xiàn)代碼4. PHP設(shè)計(jì)模式中工廠模式深入詳解5. 用css截取字符的幾種方法詳解(css排版隱藏溢出文本)6. asp中response.write("中文")或者js中文亂碼問(wèn)題7. .NET中l(wèi)ambda表達(dá)式合并問(wèn)題及解決方法8. ThinkPHP5實(shí)現(xiàn)JWT Token認(rèn)證的過(guò)程(親測(cè)可用)9. ASP 信息提示函數(shù)并作返回或者轉(zhuǎn)向10. ASP.NET MVC遍歷驗(yàn)證ModelState的錯(cuò)誤信息
