如何读取硬盘序列号 请问如何用Java读取硬件信息,比如硬盘序列号、网卡MAC地址、CPU ID等信息?有源代码吗? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 import java.io.*;import java.util.*;import java.util.regex.*;public class HardWareID { String IPCONFIG_COMMAND_WIN = "ipconfig /all"; boolean realMac = true; String unique = ""; public static String getMacAddress() { HardWareID hwid = new HardWareID(); return hwid.getUnique().trim(); } private String getUnique() { String os = System.getProperty("os.name"); if (os.startsWith("Windows")) { return getUniqueWindows(); }else { return ""; } } private String getUniqueWindows() { // 运行控制台命令,返回结果字符串 String ipConfigResponse = null; try { ipConfigResponse = runConsoleCommand(IPCONFIG_COMMAND_WIN); } catch (IOException e) { e.printStackTrace(); } // 按行分割结果字符串,并循环判断每个字符串直道找出 Mac 地址 StringTokenizer tokenizer = new StringTokenizer(ipConfigResponse, "\n"); while (tokenizer.hasMoreTokens()) { String line = tokenizer.nextToken().trim(); // 获取每行 ":" 后的字符串 int macAddressPosition = line.indexOf(":"); if (macAddressPosition <= 0) { continue; } String macAddressCandidate = line.substring(macAddressPosition + 1). trim(); // 检查 macAddressCandidate 中内容是否为真实网卡 Mac 地址,根据 Mac 地址计算出唯一标识。 if (isMacAddWin(macAddressCandidate)) { if (realMac == true) { generateUnique(macAddressCandidate); } else { realMac = true; } } } return unique; } /** * 运行控制台命令,返回结果字符串 * @param command String * @return String * @throws IOException */ private String runConsoleCommand(String command) throws IOException { Process p = Runtime.getRuntime().exec(command); InputStream stdoutStream = new BufferedInputStream(p.getInputStream()); StringBuffer buffer = new StringBuffer(); while (true) { int c = stdoutStream.read(); if (c == -1) { break; } buffer.append( (char) c); } String outputText = buffer.toString(); stdoutStream.close(); return outputText; } /** * 对输入参数进行检查,符合正则表达式的为 windows 平台下有效 Mac 地址 * @param macAddressCandidate String * @return boolean */ private boolean isMacAddWin(String macAddressCandidate) { Pattern macPattern = Pattern.compile("[0-9a-fA-F]{2}-[0-9a-fA-F]{2}-[0-9a-fA-F]{2}-[0-9a-fA-F]{2}-[0-9a-fA-F]{2}-[0-9a-fA-F]{2}"); Matcher m = macPattern.matcher(macAddressCandidate); return m.matches(); } /** * 对输入参数进行检查,符合长度的为 MAC OS X 下有效网卡 Mac 地址 * @param macAddressCandidate String * @return boolean */ private boolean isMacAddOSX(String macAddressCandidate) { if (macAddressCandidate.length() != 17) { return false; } else { return true; } } /** * 产生 Unique * @param macAddress String */ private void generateUnique(String macAddress) { if (unique == "") { unique += macAddress; } else { unique += "#"; unique += macAddress; } } public static void main(String[] args) { System.out.println(HardWareID.getMacAddress()); }} 这个方法是windows平台下通过运行控制台命令取网卡id。其实最好的方法还是使用c直接取硬件id,然后使用jni调用c的方法。另外,不是所有的硬盘,CPU都有序列号。如果要作为机器的唯一标识,建议组合使用。 Polynomial 实现 自动打包解包问题 java 运行时找不到类 请问 一天内只出现一次的概率怎么写? 谁用过jbuilder开发hibernate和spring??? 基本数据类型和对象的区别 十六进制编码如何一起转出来? utf-8编码问题 请问什么时候该用“class classname extends Object? 在JBuilde中将java代码编译成本地代码后,是否就可以脱离JRE环境 java.security.AccessControlException 急 在线等待
import java.util.*;
import java.util.regex.*;public class HardWareID {
String IPCONFIG_COMMAND_WIN = "ipconfig /all";
boolean realMac = true;
String unique = ""; public static String getMacAddress() {
HardWareID hwid = new HardWareID();
return hwid.getUnique().trim();
} private String getUnique() {
String os = System.getProperty("os.name"); if (os.startsWith("Windows")) {
return getUniqueWindows();
}else {
return "";
}
} private String getUniqueWindows() {
// 运行控制台命令,返回结果字符串
String ipConfigResponse = null;
try {
ipConfigResponse = runConsoleCommand(IPCONFIG_COMMAND_WIN);
}
catch (IOException e) {
e.printStackTrace();
} // 按行分割结果字符串,并循环判断每个字符串直道找出 Mac 地址
StringTokenizer tokenizer = new StringTokenizer(ipConfigResponse, "\n");
while (tokenizer.hasMoreTokens()) {
String line = tokenizer.nextToken().trim(); // 获取每行 ":" 后的字符串
int macAddressPosition = line.indexOf(":");
if (macAddressPosition <= 0) {
continue;
}
String macAddressCandidate = line.substring(macAddressPosition + 1).
trim();
// 检查 macAddressCandidate 中内容是否为真实网卡 Mac 地址,根据 Mac 地址计算出唯一标识。
if (isMacAddWin(macAddressCandidate)) {
if (realMac == true) {
generateUnique(macAddressCandidate);
}
else {
realMac = true;
}
}
} return unique;
}
/**
* 运行控制台命令,返回结果字符串
* @param command String
* @return String
* @throws IOException
*/
private String runConsoleCommand(String command) throws IOException {
Process p = Runtime.getRuntime().exec(command);
InputStream stdoutStream = new BufferedInputStream(p.getInputStream()); StringBuffer buffer = new StringBuffer();
while (true) {
int c = stdoutStream.read();
if (c == -1) {
break;
}
buffer.append( (char) c);
}
String outputText = buffer.toString(); stdoutStream.close(); return outputText;
} /**
* 对输入参数进行检查,符合正则表达式的为 windows 平台下有效 Mac 地址
* @param macAddressCandidate String
* @return boolean
*/
private boolean isMacAddWin(String macAddressCandidate) {
Pattern macPattern = Pattern.compile("[0-9a-fA-F]{2}-[0-9a-fA-F]{2}-[0-9a-fA-F]{2}-[0-9a-fA-F]{2}-[0-9a-fA-F]{2}-[0-9a-fA-F]{2}");
Matcher m = macPattern.matcher(macAddressCandidate);
return m.matches();
} /**
* 对输入参数进行检查,符合长度的为 MAC OS X 下有效网卡 Mac 地址
* @param macAddressCandidate String
* @return boolean
*/
private boolean isMacAddOSX(String macAddressCandidate) {
if (macAddressCandidate.length() != 17) {
return false;
}
else {
return true;
}
} /**
* 产生 Unique
* @param macAddress String
*/
private void generateUnique(String macAddress) {
if (unique == "") {
unique += macAddress;
}
else {
unique += "#";
unique += macAddress;
}
} public static void main(String[] args) {
System.out.println(HardWareID.getMacAddress());
}
}