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)限不夠,可能格式可能亂碼等問題
后續(xù)轉(zhuǎn)為網(wǎng)絡(luò)編程API接口
網(wǎng)絡(luò)適配器的 IPv4 和 MAC 地址,最好直接使用 Java 的網(wǎng)絡(luò)編程 API,而不是通過執(zhí)行系統(tǒng)命令來獲取,可以使用 java.net.NetworkInterface
類來獲取網(wǎng)絡(luò)接口的信息,然后進(jìn)一步篩選出所需的適配器信息
- 在獲取本地主機(jī)信息時,要考慮多網(wǎng)卡的情況,確保準(zhǔn)確獲取所需的網(wǎng)絡(luò)適配器信息
- 對于操作系統(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(); // 過濾 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)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java使用dom4j實(shí)現(xiàn)對xml簡單的增刪改查操作示例
這篇文章主要介紹了Java使用dom4j實(shí)現(xiàn)對xml簡單的增刪改查操作,結(jié)合實(shí)例形式詳細(xì)分析了Java使用dom4j實(shí)現(xiàn)對xml簡單的增刪改查基本操作技巧與相關(guān)注意事項(xiàng),需要的朋友可以參考下2020-05-05解決Maven無法下載2.1.7.js7版本的itext依賴問題
本文主要解決使用Maven編譯項(xiàng)目時出現(xiàn)的itext依賴版本問題,通過分析,發(fā)現(xiàn)該問題是由jasperreports依賴的特定版本itext導(dǎo)致的,解決方法是排除jasperreports中的itext依賴,并自行指定更高版本的itext依賴2024-12-12HTTP 415錯誤-Unsupported media type詳解
這篇文章主要介紹了HTTP 415錯誤-Unsupported media type詳解,本篇文章通過簡要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-08-08Java多線程之定時器Timer的實(shí)現(xiàn)
定時/計(jì)劃功能在Java應(yīng)用的各個領(lǐng)域都使用得非常多,比方說Web層面。本文主要為大家介紹了Java多線程中定時器Timer的實(shí)現(xiàn),感興趣的小伙伴可以了解一下2022-10-10Spring Boot項(xiàng)目實(shí)戰(zhàn)之?dāng)r截器與過濾器
這篇文章主要介紹了Spring Boot項(xiàng)目實(shí)戰(zhàn)之?dāng)r截器與過濾器,文中給大家詳細(xì)介紹了springboot 攔截器和過濾器的基本概念,過濾器的配置,需要的朋友可以參考下2018-01-01