Java獲取IP地址以及MAC地址的示例代碼
前言
需要獲取客戶端的IP地址以及MAC地址
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class test {
public static void main(String[] args) {
try {
// 執(zhí)行命令
Process process = Runtime.getRuntime().exec("ipconfig /all");
// 讀取命令輸出
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line;
while ((line = reader.readLine()) != null) {
// 輸出命令結果
System.out.println(line);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
純用CMD可能權限不夠,可能格式可能亂碼等問題

后續(xù)轉(zhuǎn)為網(wǎng)絡編程API接口
網(wǎng)絡適配器的 IPv4 和 MAC 地址,最好直接使用 Java 的網(wǎng)絡編程 API,而不是通過執(zhí)行系統(tǒng)命令來獲取,可以使用 java.net.NetworkInterface 類來獲取網(wǎng)絡接口的信息,然后進一步篩選出所需的適配器信息
- 在獲取本地主機信息時,要考慮多網(wǎng)卡的情況,確保準確獲取所需的網(wǎng)絡適配器信息
- 對于操作系統(tǒng)信息的獲取,可以考慮使用更可靠的方式,如 System.getProperty() 方法
1. IP及MAC
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;
public class test {
public static void main(String[] args) {
try {
// 獲取所有網(wǎng)絡接口
Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
while (interfaces.hasMoreElements()) {
NetworkInterface networkInterface = interfaces.nextElement();
// 輸出網(wǎng)絡接口名稱
System.out.println("Network Interface: " + networkInterface.getDisplayName());
// 獲取該網(wǎng)絡接口的所有地址
Enumeration<InetAddress> addresses = networkInterface.getInetAddresses();
while (addresses.hasMoreElements()) {
InetAddress address = addresses.nextElement();
// 輸出地址信息
System.out.println(" Address: " + address.getHostAddress());
}
// 獲取 MAC 地址
byte[] mac = networkInterface.getHardwareAddress();
if (mac != null) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < mac.length; i++) {
sb.append(String.format("%02X%s", mac[i], (i < mac.length - 1) ? "-" : ""));
}
// 輸出 MAC 地址
System.out.println(" MAC Address: " + sb.toString());
}
}
} catch (SocketException e) {
e.printStackTrace();
}
}
}
截圖如下:

2. 特定適配器
不同電腦可能特定的適配器不大一樣,具體Demo看自身
package com.example.test;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;
public class test {
public static void main(String[] args) {
try {
// 獲取所有網(wǎng)絡接口
Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
while (interfaces.hasMoreElements()) {
NetworkInterface networkInterface = interfaces.nextElement();
// 檢查是否是你想要的網(wǎng)絡適配器,這里假設名字為 "VMware Network Adapter VMnet8"
if (networkInterface.getDisplayName().equals("VMware Virtual Ethernet Adapter for VMnet8")) {
// 獲取該網(wǎng)絡接口的所有地址
Enumeration<InetAddress> addresses = networkInterface.getInetAddresses();
while (addresses.hasMoreElements()) {
InetAddress address = addresses.nextElement();
// 過濾 IPv4 地址
if (address instanceof java.net.Inet4Address) {
// 輸出 IPv4 地址
System.out.println("IPv4 Address: " + address.getHostAddress());
}
}
// 獲取 MAC 地址
byte[] mac = networkInterface.getHardwareAddress();
if (mac != null) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < mac.length; i++) {
sb.append(String.format("%02X%s", mac[i], (i < mac.length - 1) ? "-" : ""));
}
// 輸出 MAC 地址
System.out.println("MAC Address: " + sb.toString());
}
}
}
} catch (SocketException e) {
e.printStackTrace();
}
}
}
截圖如下:

到此這篇關于Java獲取IP地址以及MAC地址的示例代碼的文章就介紹到這了,更多相關Java獲取IP地址以及MAC地址內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Java使用dom4j實現(xiàn)對xml簡單的增刪改查操作示例
這篇文章主要介紹了Java使用dom4j實現(xiàn)對xml簡單的增刪改查操作,結合實例形式詳細分析了Java使用dom4j實現(xiàn)對xml簡單的增刪改查基本操作技巧與相關注意事項,需要的朋友可以參考下2020-05-05
解決Maven無法下載2.1.7.js7版本的itext依賴問題
本文主要解決使用Maven編譯項目時出現(xiàn)的itext依賴版本問題,通過分析,發(fā)現(xiàn)該問題是由jasperreports依賴的特定版本itext導致的,解決方法是排除jasperreports中的itext依賴,并自行指定更高版本的itext依賴2024-12-12
HTTP 415錯誤-Unsupported media type詳解
這篇文章主要介紹了HTTP 415錯誤-Unsupported media type詳解,本篇文章通過簡要的案例,講解了該項技術的了解與使用,以下就是詳細內(nèi)容,需要的朋友可以參考下2021-08-08

