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) { // 輸出命令結(jié)果 System.out.println(line); } } catch (IOException e) { e.printStackTrace(); } } }
純用CMD可能權(quán)限不夠,可能格式可能亂碼等問(wèn)題
后續(xù)轉(zhuǎn)為網(wǎng)絡(luò)編程API接口
網(wǎng)絡(luò)適配器的 IPv4 和 MAC 地址,最好直接使用 Java 的網(wǎng)絡(luò)編程 API,而不是通過(guò)執(zhí)行系統(tǒng)命令來(lái)獲取,可以使用 java.net.NetworkInterface
類來(lái)獲取網(wǎng)絡(luò)接口的信息,然后進(jìn)一步篩選出所需的適配器信息
- 在獲取本地主機(jī)信息時(shí),要考慮多網(wǎng)卡的情況,確保準(zhǔn)確獲取所需的網(wǎng)絡(luò)適配器信息
- 對(duì)于操作系統(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)絡(luò)接口 Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces(); while (interfaces.hasMoreElements()) { NetworkInterface networkInterface = interfaces.nextElement(); // 輸出網(wǎng)絡(luò)接口名稱 System.out.println("Network Interface: " + networkInterface.getDisplayName()); // 獲取該網(wǎng)絡(luò)接口的所有地址 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)絡(luò)接口 Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces(); while (interfaces.hasMoreElements()) { NetworkInterface networkInterface = interfaces.nextElement(); // 檢查是否是你想要的網(wǎng)絡(luò)適配器,這里假設(shè)名字為 "VMware Network Adapter VMnet8" if (networkInterface.getDisplayName().equals("VMware Virtual Ethernet Adapter for VMnet8")) { // 獲取該網(wǎng)絡(luò)接口的所有地址 Enumeration<InetAddress> addresses = networkInterface.getInetAddresses(); while (addresses.hasMoreElements()) { InetAddress address = addresses.nextElement(); // 過(guò)濾 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(); } } }
截圖如下:
到此這篇關(guān)于Java獲取IP地址以及MAC地址的示例代碼的文章就介紹到這了,更多相關(guān)Java獲取IP地址以及MAC地址內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java使用dom4j實(shí)現(xiàn)對(duì)xml簡(jiǎn)單的增刪改查操作示例
這篇文章主要介紹了Java使用dom4j實(shí)現(xiàn)對(duì)xml簡(jiǎn)單的增刪改查操作,結(jié)合實(shí)例形式詳細(xì)分析了Java使用dom4j實(shí)現(xiàn)對(duì)xml簡(jiǎn)單的增刪改查基本操作技巧與相關(guān)注意事項(xiàng),需要的朋友可以參考下2020-05-05解決Maven無(wú)法下載2.1.7.js7版本的itext依賴問(wèn)題
本文主要解決使用Maven編譯項(xiàng)目時(shí)出現(xiàn)的itext依賴版本問(wèn)題,通過(guò)分析,發(fā)現(xiàn)該問(wèn)題是由jasperreports依賴的特定版本itext導(dǎo)致的,解決方法是排除jasperreports中的itext依賴,并自行指定更高版本的itext依賴2024-12-12HTTP 415錯(cuò)誤-Unsupported media type詳解
這篇文章主要介紹了HTTP 415錯(cuò)誤-Unsupported media type詳解,本篇文章通過(guò)簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-08-08使用maven打包/跳過(guò)某個(gè)modules
本文總結(jié)了在Maven項(xiàng)目中跳過(guò)或單獨(dú)構(gòu)建模塊的方法,包括使用`-pl`、`-am`和`-amd`參數(shù)來(lái)選擇性地執(zhí)行模塊構(gòu)建,以及通過(guò)`-Dmaven.test.skip`跳過(guò)測(cè)試,以提高構(gòu)建效率2024-12-12mybatis?foreach傳兩個(gè)參數(shù)批量刪除
這篇文章主要介紹了mybatis?foreach?批量刪除傳兩個(gè)參數(shù),本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-04-04MyBatis-Plus自定義SQL的詳細(xì)過(guò)程記錄
Java開(kāi)發(fā)使用mybatis-plus來(lái)執(zhí)行sql操作,往往比mybatis能夠省時(shí)省力,下面這篇文章主要給大家介紹了關(guān)于MyBatis-Plus自定義SQL的相關(guān)資料,需要的朋友可以參考下2022-02-02Java多線程之定時(shí)器Timer的實(shí)現(xiàn)
定時(shí)/計(jì)劃功能在Java應(yīng)用的各個(gè)領(lǐng)域都使用得非常多,比方說(shuō)Web層面。本文主要為大家介紹了Java多線程中定時(shí)器Timer的實(shí)現(xiàn),感興趣的小伙伴可以了解一下2022-10-10Spring Boot項(xiàng)目實(shí)戰(zhàn)之?dāng)r截器與過(guò)濾器
這篇文章主要介紹了Spring Boot項(xiàng)目實(shí)戰(zhàn)之?dāng)r截器與過(guò)濾器,文中給大家詳細(xì)介紹了springboot 攔截器和過(guò)濾器的基本概念,過(guò)濾器的配置,需要的朋友可以參考下2018-01-01