1. 程式人生 > >java獲取硬碟ID以及MAC地址等唯一標識碼(詳解,測試通過)

java獲取硬碟ID以及MAC地址等唯一標識碼(詳解,測試通過)

為了達到軟體註冊,或者說軟體和電腦繫結的目的,需要將電腦上的固定編號進行一系列的演算法計算,並生成唯一和軟體匹配的號碼。

那麼使用java如何達到這個目的呢?

通常做法都是通過java的Runtime來完成,通過 process的輸入流,進行獲取相關的資訊。

下面列舉具體的例子:

 1、DiskUtils 獲取硬碟編號

  1. import java.io.File;  
  2. import java.io.FileWriter;  
  3. import java.io.BufferedReader;  
  4. import java.io.InputStreamReader;  
  5. class DiskUtils {  
  6.     private
     DiskUtils() {  
  7.     }  
  8.     publicstatic String getSerialNumber(String drive) {  
  9.         String result = "";  
  10.         try {  
  11.             File file = File.createTempFile("damn"".vbs");  
  12.             file.deleteOnExit();  
  13.             FileWriter fw = new java.io.FileWriter(file);  
  14.             String vbs = "Set objFSO = CreateObject(\"Scripting.FileSystemObject\")\n"
  15.                     + "Set colDrives = objFSO.Drives\n"
  16.                     + "Set objDrive = colDrives.item(\""
  17.                     + drive  
  18.                     + "\")\n"
  19.                     + "Wscript.Echo objDrive.SerialNumber"// see note
  20.             fw.write(vbs);  
  21.             fw.close();  
  22.             Process p = Runtime.getRuntime().exec(  
  23.                     "cscript //NoLogo " + file.getPath());  
  24.             BufferedReader input = new BufferedReader(new InputStreamReader(  
  25.                     p.getInputStream()));  
  26.             String line;  
  27.             while ((line = input.readLine()) != null) {  
  28.                 result += line;  
  29.             }  
  30.             input.close();  
  31.         } catch (Exception e) {  
  32.             e.printStackTrace();  
  33.         }  
  34.         return result.trim();  
  35.     }  
  36. }  

2、MacUtils 獲取MAC地址

  1. import java.io.InputStreamReader;  
  2. import java.io.LineNumberReader;  
  3. publicclass MacUtils {  
  4.     publicstaticvoid  getMac(){  
  5.     try {  
  6.         Process process = Runtime.getRuntime().exec("ipconfig /all");  
  7.         InputStreamReader ir = new InputStreamReader(process.getInputStream());  
  8.         LineNumberReader input = new LineNumberReader(ir);  
  9.         String line;  
  10.         while ((line = input.readLine()) != null)  
  11.         if (line.indexOf("Physical Address") > 0) {  
  12.             String MACAddr = line.substring(line.indexOf("-") - 2);  
  13.             System.out.println("MAC address = [" + MACAddr + "]");  
  14.         }  
  15.         } catch (java.io.IOException e) {  
  16.             System.err.println("IOException " + e.getMessage());  
  17.         }  
  18.     }  
  19. }  


3、 測試程式:

  1. import java.io.InputStreamReader;  
  2. import java.io.LineNumberReader;  
  3. import java.net.NetworkInterface;  
  4. import java.net.SocketException;  
  5. import java.util.Enumeration;  
  6. import java.util.Vector;  
  7. publicclass TestMain {  
  8.     publicstaticvoid main(String[] args) {  
  9.         // TODO Auto-generated method stub
  10.         //
  11.         System.out.println("***MAC地址***");  
  12.         MacUtils.getMac();  
  13.         //
  14.         String sn = DiskUtils.getSerialNumber("C");   
  15.         System.out.println("***硬碟編號***");  
  16.         System.out.println(sn);   
  17.     }  
  18. }  


4、執行結果(我電腦上有幾個VPN,所以就有多個MAC;為了防止別人搞我的電腦,數字和字母用*號代替)

***MAC地址***
MAC address = [**-**-**-**-**-**]
MAC address = [**-**-**-**-**-**]
MAC address =[**-**-**-**-**-**]
MAC address = [**-**-**-**-**-**]
***硬碟編號***
1290******