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

来源:互联网 发布:黄黄的软件 编辑:程序博客网 时间:2024/04/25 09:16

今天在完成任务的时候,遇到了需要对对应的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. }
原创粉丝点击