日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

java 取cpuid、主板id、硬盘id、mac地址

發布時間:2025/7/14 编程问答 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java 取cpuid、主板id、硬盘id、mac地址 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
import java.io.BufferedReader;
import java.io.File;
import java.io.FileWriter;
import java.io.InputStreamReader;
import java.io.LineNumberReader;


public class HardWareUtils {


/**
* 獲取主板序列號
*?
* @return
*/
public static String getMotherboardSN() {
String result = "";
try {
File file = File.createTempFile("realhowto", ".vbs");
file.deleteOnExit();
FileWriter fw = new java.io.FileWriter(file);


String vbs = "Set objWMIService = GetObject(\"winmgmts:\\\\.\\root\\cimv2\")\n"
+ "Set colItems = objWMIService.ExecQuery _ \n"
+ " ? (\"Select * from Win32_BaseBoard\") \n"
+ "For Each objItem in colItems \n"
+ " ? ?Wscript.Echo objItem.SerialNumber \n"
+ " ? ?exit for ?' do the first cpu only! \n" + "Next \n";


fw.write(vbs);
fw.close();
Process p = Runtime.getRuntime().exec(
"cscript //NoLogo " + file.getPath());
BufferedReader input = new BufferedReader(new InputStreamReader(
p.getInputStream()));
String line;
while ((line = input.readLine()) != null) {
result += line;
}
input.close();
} catch (Exception e) {
e.printStackTrace();
}
return result.trim();
}


/**
* 獲取硬盤序列號
*?
* @param drive
* ? ? ? ? ? ?盤符
* @return
*/
public static String getHardDiskSN(String drive) {
String result = "";
try {
File file = File.createTempFile("realhowto", ".vbs");
file.deleteOnExit();
FileWriter fw = new java.io.FileWriter(file);


String vbs = "Set objFSO = CreateObject(\"Scripting.FileSystemObject\")\n"
+ "Set colDrives = objFSO.Drives\n"
+ "Set objDrive = colDrives.item(\""
+ drive
+ "\")\n"
+ "Wscript.Echo objDrive.SerialNumber"; // see note
fw.write(vbs);
fw.close();
Process p = Runtime.getRuntime().exec(
"cscript //NoLogo " + file.getPath());
BufferedReader input = new BufferedReader(new InputStreamReader(
p.getInputStream()));
String line;
while ((line = input.readLine()) != null) {
result += line;
}
input.close();
} catch (Exception e) {
e.printStackTrace();
}
return result.trim();
}


/**
* 獲取CPU序列號
*?
* @return
*/
public static String getCPUSerial() {
String result = "";
try {
File file = File.createTempFile("tmp", ".vbs");
file.deleteOnExit();
FileWriter fw = new java.io.FileWriter(file);
String vbs = "Set objWMIService = GetObject(\"winmgmts:\\\\.\\root\\cimv2\")\n"
+ "Set colItems = objWMIService.ExecQuery _ \n"
+ " ? (\"Select * from Win32_Processor\") \n"
+ "For Each objItem in colItems \n"
+ " ? ?Wscript.Echo objItem.ProcessorId \n"
+ " ? ?exit for ?' do the first cpu only! \n" + "Next \n";


// + " ? ?exit for ?\r\n" + "Next";
fw.write(vbs);
fw.close();
Process p = Runtime.getRuntime().exec(
"cscript //NoLogo " + file.getPath());
BufferedReader input = new BufferedReader(new InputStreamReader(
p.getInputStream()));
String line;
while ((line = input.readLine()) != null) {
result += line;
}
input.close();
file.delete();
} catch (Exception e) {
e.fillInStackTrace();
}
if (result.trim().length() < 1 || result == null) {
result = "無CPU_ID被讀取";
}
return result.trim();
}


/**
* 獲取MAC地址
*/
public static String getMac(){
String result = "";
? ?try { ?
?
? ? ? ?Process process = Runtime.getRuntime().exec("ipconfig /all"); ?
?
? ? ? ?InputStreamReader ir = new InputStreamReader(process.getInputStream()); ?
?
? ? ? ?LineNumberReader input = new LineNumberReader(ir); ?
?
? ? ? ?String line; ?
?
? ? ? ?while ((line = input.readLine()) != null) ?
? ? ? ? ? ? ?
?
? ? ? ?if (line.indexOf("Physical Address") > 0) { ?
?
? ? ? ? ? ?String MACAddr = line.substring(line.indexOf("-") - 2); ?
?
? ? ? ? ? ?result=MACAddr; ?
?
? ? ? ?} ?
?
? ? ? ?} catch (java.io.IOException e) { ?
?
? ? ? ? ? ?System.err.println("IOException " + e.getMessage()); ?
?
? ? ? ?} ?
? ? ? ?return result;
? ?} ?

public static void main(String[] args) {
System.out.println("CPU ?SN:"+HardWareUtils.getCPUSerial());
System.out.println("主板 ?SN:"+HardWareUtils.getMotherboardSN());
System.out.println("C盤 ? SN:"+HardWareUtils.getHardDiskSN("c"));
System.out.println("MAC ?SN:"+HardWareUtils.getMac());
}

}

總結

以上是生活随笔為你收集整理的java 取cpuid、主板id、硬盘id、mac地址的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。