午夜剧场伦理_日本一道高清_国产又黄又硬_91黄色网战_女同久久另类69精品国产_妹妹的朋友在线

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

基于Java實(shí)現(xiàn)獲取本地IP地址和主機(jī)名

瀏覽:21日期:2022-09-01 17:00:39

方式一:通過(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)。

標(biāo)簽: Java
相關(guān)文章:
主站蜘蛛池模板: 丰满肉嫩西川结衣av | 91毛片网站| 日韩一区2区 | 日本一级一片免费视频 | 久久综合视频网 | 超碰久操 | 一区二区视屏 | 日本欧美一区 | 国产精品爽 | 日韩中文字幕在线观看视频 | 精品国产乱码久久久久久久 | 亚洲第一色站 | 色婷婷亚洲综合 | 美日韩在线 | 成人免费专区 | 亚洲精选一区二区 | 超碰97色| 中文字幕理论片 | 亚洲一区二区av在线 | 国产精品久久久免费观看 | 天天操婷婷 | 成人在线免费观看视频 | 99性视频 | 色综综 | 中国美女黄色一级片 | 一区二区三区国产在线 | 九九在线精品视频 | 日日综合 | 日韩精品大片 | 免费观看一区二区三区 | 国产精品成人久久久 | 在线免费看毛片 | 天天干天天玩 | 男女全黄做爰文章 | 一区二区精彩视频 | 久久视频网站 | 免费一级黄色大片 | 欧美日韩中文字幕一区二区三区 | 久久99精品国产麻豆婷婷洗澡 | 精品国产专区 | 欧美日韩亚洲在线观看 |