这个得调用c/c++之后再调用系统函数获取。通过JNI来获取吧。
解决方案 »
- java解析dbf格式时里面数据类型怎么判断,急急。。
- java垃圾回收的疑问(new class.forname classloader)
- 多级下拉列表
- 关于MyEclipse装完svn之后不能创建Web Project的问题
- 有谁要国外的一套开源搜索引擎项目(Java)?
- PowerCenter 9 数据增量抽取
- hibernate junit测试数据操作室老报错。。
- 在struts-config.xml中配置datasource出错,求助!
- ecplise的参数怎么设置?
- J2EE相关设计模式讨论: 序
- 多行文本的换行显示问题,怎么实现文本中固有的换行啊?
- Tomcat无法启动,大家帮忙看看,先谢了.
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都有序列号。如果要作为机器的唯一标识,建议组合使用。