JAVA獲取本地MAC地址的方法
阿新 • • 發佈:2020-11-27
InetAddress物件
此類表示Internet協議(IP)地址。
IP地址是由IP使用的32位或128位無符號數字,構建UDP和TCP協議的低階協議。 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地址和可能的相應主機名組成(取決於它是用主機名構造還是已經完成了反向主機名解析)。
NetworkInterface物件
此類表示由名稱組成的網路介面和分配給此介面的IP地址列表。 用於標識組播組所在的本地介面。 介面通常由諸如“le0”的名稱所知。
程式碼
/** * @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 { // 獲得網路介面物件(即網絡卡),並得到mac地址,mac地址存在於一個byte陣列中。 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轉化為正整數 String s = Integer.toHexString(mac[i] & 0xFF); sb.append(s.length() == 1 ? 0 + s : s); } // 把字串所有小寫字母改為大寫成為正規的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次的執行時間 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 { // 獲得網路介面物件(即網絡卡),並得到mac地址,mac地址存在於一個byte陣列中。 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轉化為正整數 String s = Integer.toHexString(mac[i] & 0xFF); sb.append(s.length() == 1 ? 0 + s : s); } // 把字串所有小寫字母改為大寫成為正規的mac地址並返回 return sb.toString().toUpperCase(); } }
執行結果:
建議
從執行結果來看,java獲取本地MAC地址還是挺慢的。因為MAC不會輕易改變,所以可以考慮在專案執行時獲取一次MAC地址,然後存放到快取中,用到MAC地址時從快取中取,提高效率。
到此這篇關於JAVA獲取本地MAC地址的方法的文章就介紹到這了,更多相關JAVA獲取MAC地址內容請搜尋我們以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援我們!