着急的问题:怎样用java得到硬盘的序列号!!! 请问各位大虾,怎样用java得到硬盘的序列号啊?先谢谢了,急用!:) 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 估计用java无法直接得到硬盘序列号,不过用c,或c++编写取得硬盘序列号的程序做成DLL,然后再java里面用native方法访问的话,应该可以。 使用jni还是比较麻烦,不如让别人写个exe程序(windows下),让后java程序运行一下,得到序列号就行了。 看一眼:http://www.sz3000.com/index.htm?QQ=822982 http://community.csdn.net/expert/TopicView.asp?id=4254059import 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都有序列号。如果要作为机器的唯一标识,建议组合使用。 关于 EXCEL 大量数据库导入 求方案 关于winxp下无法写入文档的问题 应该怎么样把sql写到java里 java如何获取网卡数据? jdk1.5 General Type 的问题, 请问如何做数据类型转换 如何使JFrame窗口不显示 标题栏?? 运行是出错!NoClassDefFoundError,请大家帮忙! 问一个关于JDialog的简单问题 我是一名新手,请问如何学java 急!!!求助!!java下怎么能取得本地的cpu序号或硬盘的id号???? gbk(系列)的编码方法,并没有涵盖iso8859-1,而只是涵盖了ascii?高手也进来看看
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都有序列号。如果要作为机器的唯一标识,建议组合使用。