Java中獲取windows、Linux和windows7的MAC地址

今天在完成任務的時候,遇到了需要對對應的MAC地址進行驗證的方法,以爲很簡單就能過,鼓搗了半天以後才發現,我的機器是window7,查詢出來是亂碼,居然不給支持。沒辦法在網上繼續找資料。終於找到了,貼上來,以備不時之需。 

東西都有註釋,自己看吧, 
Java代碼  收藏代碼
  1. import java.io.BufferedReader;  
  2. import java.io.IOException;  
  3. import java.io.InputStreamReader;  
  4. import java.net.InetAddress;  
  5. import java.net.NetworkInterface;  
  6.   
  7. /** 
  8.  * 與系統相關的一些常用工具方法. 
  9.  *  
  10.  * @author lvbogun 
  11.  * @version 1.0.0 
  12.  */  
  13. public class SystemTool {  
  14.   
  15.     /** 
  16.      * 獲取當前操作系統名稱. return 操作系統名稱 例如:windows xp,linux 等. 
  17.      */  
  18.     public static String getOSName() {  
  19.         return System.getProperty("os.name").toLowerCase();  
  20.     }  
  21.   
  22.     /** 
  23.      * 獲取unix網卡的mac地址. 非windows的系統默認調用本方法獲取. 
  24.      * 如果有特殊系統請繼續擴充新的取mac地址方法. 
  25.      *  
  26.      * @return mac地址 
  27.      */  
  28.     public static String getUnixMACAddress() {  
  29.         String mac = null;  
  30.         BufferedReader bufferedReader = null;  
  31.         Process process = null;  
  32.         try {  
  33.             // linux下的命令,一般取eth0作爲本地主網卡  
  34.             process = Runtime.getRuntime().exec("ifconfig eth0");  
  35.             // 顯示信息中包含有mac地址信息  
  36.             bufferedReader = new BufferedReader(new InputStreamReader(  
  37.                     process.getInputStream()));  
  38.             String line = null;  
  39.             int index = -1;  
  40.             while ((line = bufferedReader.readLine()) != null) {  
  41.                 // 尋找標示字符串[hwaddr]  
  42.                 index = line.toLowerCase().indexOf("hwaddr");  
  43.                 if (index >= 0) {// 找到了  
  44.                     // 取出mac地址並去除2邊空格  
  45.                     mac = line.substring(index + "hwaddr".length() + 1).trim();  
  46.                     break;  
  47.                 }  
  48.             }  
  49.         } catch (IOException e) {  
  50.             e.printStackTrace();  
  51.         } finally {  
  52.             try {  
  53.                 if (bufferedReader != null) {  
  54.                     bufferedReader.close();  
  55.                 }  
  56.             } catch (IOException e1) {  
  57.                 e1.printStackTrace();  
  58.             }  
  59.             bufferedReader = null;  
  60.             process = null;  
  61.         }  
  62.         return mac;  
  63.     }  
  64.   
  65.     /** 
  66.      * 獲取widnows網卡的mac地址. 
  67.      *  
  68.      * @return mac地址 
  69.      */  
  70.     public static String getWindowsMACAddress() {  
  71.         String mac = null;  
  72.         BufferedReader bufferedReader = null;  
  73.         Process process = null;  
  74.         try {  
  75.             // windows下的命令,顯示信息中包含有mac地址信息  
  76.             process = Runtime.getRuntime().exec("ipconfig /all");  
  77.             bufferedReader = new BufferedReader(new InputStreamReader(  
  78.                     process.getInputStream()));  
  79.             String line = null;  
  80.             int index = -1;  
  81.             while ((line = bufferedReader.readLine()) != null) {  
  82.                 System.out.println(line);  
  83.                 // 尋找標示字符串[physical  
  84.                 index = line.toLowerCase().indexOf("physical address");  
  85.                   
  86.                 if (index >= 0) {// 找到了  
  87.                     index = line.indexOf(":");// 尋找":"的位置  
  88.                     if (index >= 0) {  
  89.                         System.out.println(mac);  
  90.                         // 取出mac地址並去除2邊空格  
  91.                         mac = line.substring(index + 1).trim();  
  92.                     }  
  93.                     break;  
  94.                 }  
  95.             }  
  96.         } catch (IOException e) {  
  97.             e.printStackTrace();  
  98.         } finally {  
  99.             try {  
  100.                 if (bufferedReader != null) {  
  101.                     bufferedReader.close();  
  102.                 }  
  103.             } catch (IOException e1) {  
  104.                 e1.printStackTrace();  
  105.             }  
  106.             bufferedReader = null;  
  107.             process = null;  
  108.         }  
  109.   
  110.         return mac;  
  111.     }  
  112.   
  113.     /** 
  114.      * windows 7 專用 獲取MAC地址 
  115.      *  
  116.      * @return 
  117.      * @throws Exception 
  118.      */  
  119.     public static String getMACAddress() throws Exception {  
  120.           
  121.         // 獲取本地IP對象  
  122.         InetAddress ia = InetAddress.getLocalHost();  
  123.         // 獲得網絡接口對象(即網卡),並得到mac地址,mac地址存在於一個byte數組中。  
  124.         byte[] mac = NetworkInterface.getByInetAddress(ia).getHardwareAddress();  
  125.   
  126.         // 下面代碼是把mac地址拼裝成String  
  127.         StringBuffer sb = new StringBuffer();  
  128.   
  129.         for (int i = 0; i < mac.length; i++) {  
  130.             if (i != 0) {  
  131.                 sb.append("-");  
  132.             }  
  133.             // mac[i] & 0xFF 是爲了把byte轉化爲正整數  
  134.             String s = Integer.toHexString(mac[i] & 0xFF);  
  135.             sb.append(s.length() == 1 ? 0 + s : s);  
  136.         }  
  137.   
  138.         // 把字符串所有小寫字母改爲大寫成爲正規的mac地址並返回  
  139.         return sb.toString().toUpperCase();  
  140.     }  
  141.   
  142.     /** 
  143.      * 測試用的main方法. 
  144.      *  
  145.      * @param argc 運行參數. 
  146.      * @throws Exception 
  147.      */  
  148.     public static void main(String[] argc) throws Exception {  
  149.         String os = getOSName();  
  150.         System.out.println(os);  
  151.         if (os.equals("windows 7")) {  
  152.             String mac = getMACAddress();  
  153.             System.out.println(mac);  
  154.         } else if (os.startsWith("windows")) {  
  155.             // 本地是windows  
  156.             String mac = getWindowsMACAddress();  
  157.             System.out.println(mac);  
  158.         } else {  
  159.             // 本地是非windows系統 一般就是unix  
  160.             String mac = getUnixMACAddress();  
  161.             System.out.println(mac);  
  162.         }  
  163.     }  
  164. }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章