我知道你可以使用Java 6来做到这一点java.net.NetworkInterface->getHardwareAddress().但是我部署的环境仅限于Java 5.
有人知道如何在Java 5或更早版本中执行此操作吗?非常感谢.
Java 5中的标准方法是启动本机进程来运行ipconfig或ifconfig解析OutputStream以获得答案.
例如:
private String getMacAddress() throws IOException {
String command = “ipconfig /all”;
Process pid = Runtime.getRuntime().exec(command);
BufferedReader in = new BufferedReader(new InputStreamReader(pid.getInputStream()));
Pattern p = Pattern.compile(”.*Physical Address.*: (.*)”);
while (true) {
String line = in.readLine();
if (line == null)
break;
Matcher m = p.matcher(line);
if (m.matches()) {
return m.group(1);
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7828 次 |
| 最近记录: |