JAVA獲取本地MAC地址的方法
InetAddress對象
此類表示Internet協(xié)議(IP)地址。
IP地址是由IP使用的32位或128位無符號數(shù)字,構建UDP和TCP協(xié)議的低級協(xié)議。 IP地址結構由定義RFC 790: Assigned Numbers , RFC 1918: Address Allocation for Private Internets , RFC 2365: Administratively Scoped IP Multicast和RFC 2373: IP Version 6 Addressing Architecture 。 InetAddress的一個實例由一個IP地址和可能的相應主機名組成(取決于它是用主機名構造還是已經(jīng)完成了反向主機名解析)。

NetworkInterface對象
此類表示由名稱組成的網(wǎng)絡接口和分配給此接口的IP地址列表。 用于標識組播組所在的本地接口。 接口通常由諸如“l(fā)e0”的名稱所知。

代碼
/**
* @Title: getMACAddress
* @Description: 通過InetAddress對象獲取MAC地址
* @param inetAddress
* @return
* @throws Exception String
* @author: wangyk
* @date: 2020年11月23日 上午10:24:42
* @version: 2.0.1
*/
private static String getMACAddress(InetAddress inetAddress) throws Exception {
// 獲得網(wǎng)絡接口對象(即網(wǎng)卡),并得到mac地址,mac地址存在于一個byte數(shù)組中。
byte[] mac = NetworkInterface.getByInetAddress(inetAddress).getHardwareAddress();
// 下面代碼是把mac地址拼裝成String
StringBuffer sb = new StringBuffer();
for (int i = 0; i < mac.length; i++) {
if (i != 0) {
sb.append("-");
}
// mac[i] & 0xFF 是為了把byte轉(zhuǎn)化為正整數(shù)
String s = Integer.toHexString(mac[i] & 0xFF);
sb.append(s.length() == 1 ? 0 + s : s);
}
// 把字符串所有小寫字母改為大寫成為正規(guī)的mac地址并返回
return sb.toString().toUpperCase();
}
演示
/**
* @Title: Test.java
* @Description: 測試獲取本地ip
* @author: wangyk
* @date: 2020年11月23日 上午10:21:13
* @version: 2.0.1
*/
package com.yike.datamigration;
import java.net.InetAddress;
import java.net.NetworkInterface;
/**
* @Title: Test.java
* @Description: 測試獲取本地ip
* @author: wangyk
* @date: 2020年11月23日 上午10:21:13
* @version: 2.0.1
*/
public class Test {
/**
* @Title: main
* @Description: 程序的入口
* @param args
* @throws Exception void
* @author: wangyk
* @date: 2020年11月23日 上午10:25:25
* @version: 2.0.1
*/
public static void main(String[] args) throws Exception {
// 獲取本機的InetAddress對象
InetAddress localHost = InetAddress.getLocalHost();
// 記錄開始時間
long start = System.currentTimeMillis();
// 測試獲取100次的執(zhí)行時間
for (int i = 0; i < 100; i++) {
String mac = getMACAddress(localHost);
System.out.println(i + " " + mac);
}
// 記錄結束時間
long end = System.currentTimeMillis();
System.out.println("總耗時: " + (end - start));
}
/**
* @Title: getMACAddress
* @Description: 通過InetAddress對象獲取MAC地址
* @param inetAddress
* @return
* @throws Exception String
* @author: wangyk
* @date: 2020年11月23日 上午10:24:42
* @version: 2.0.1
*/
private static String getMACAddress(InetAddress inetAddress) throws Exception {
// 獲得網(wǎng)絡接口對象(即網(wǎng)卡),并得到mac地址,mac地址存在于一個byte數(shù)組中。
byte[] mac = NetworkInterface.getByInetAddress(inetAddress).getHardwareAddress();
// 下面代碼是把mac地址拼裝成String
StringBuffer sb = new StringBuffer();
for (int i = 0; i < mac.length; i++) {
if (i != 0) {
sb.append("-");
}
// mac[i] & 0xFF 是為了把byte轉(zhuǎn)化為正整數(shù)
String s = Integer.toHexString(mac[i] & 0xFF);
sb.append(s.length() == 1 ? 0 + s : s);
}
// 把字符串所有小寫字母改為大寫成為正規(guī)的mac地址并返回
return sb.toString().toUpperCase();
}
}
運行結果:

建議
從運行結果來看,java獲取本地MAC地址還是挺慢的。因為MAC不會輕易改變,所以可以考慮在項目運行時獲取一次MAC地址,然后存放到緩存中,用到MAC地址時從緩存中取,提高效率。
到此這篇關于JAVA獲取本地MAC地址的方法的文章就介紹到這了,更多相關JAVA獲取MAC地址內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
關于Java中使用jdbc連接數(shù)據(jù)庫中文出現(xiàn)亂碼的問題
這篇文章主要介紹了關于Java中使用jdbc連接數(shù)據(jù)庫中文出現(xiàn)亂碼的問題,默認的編碼和數(shù)據(jù)庫表中的數(shù)據(jù)使用的編碼是不一致的,如果是中文,那么在數(shù)據(jù)庫中執(zhí)行時已經(jīng)是亂碼了,需要的朋友可以參考下2023-04-04
@Valid注解的作用及@Valid注解與@Validated的區(qū)別
這篇文章主要介紹了@Valid注解的作用及@Valid注解與@Validated的區(qū)別,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-08-08
SpringBoot集成P6Spy實現(xiàn)SQL日志的記錄詳解
P6Spy是一個框架,它可以無縫地攔截和記錄數(shù)據(jù)庫活動,而無需更改現(xiàn)有應用程序的代碼。一般我們使用的比較多的是使用p6spy打印我們最后執(zhí)行的sql語句2022-11-11

