java获得客户端的MAC地址
java获得客户端的MAC地址如何获得客户端的MAC地址?
Java本身没有这个能力,不过利用操作系统的命令可以做到。同时我们可以通过这个例子来想想平时对我们有用的更多东西,很久以前就想弄弄这个了!现在有时间弄了一个例子和大家一起分享一下吧:
public static String getMACAddress() {
String address = "";
String os = System.getProperty("os.name");
if ( os != null && os.startsWith("Windows")) {
try {
String command = "cmd.exe /c ipconfig /all";
Process p = Runtime.getRuntime().exec(command);
BufferedReader br =
new BufferedReader(
new InputStreamReader(p.getInputStream()));
String line;
while ((line = br.readLine()) != null) {
if (line.indexOf("Physical Address") > 0) {
int index = line.indexOf(":");
index += 2;
address = line.substring(index);
break;
}
}
br.close();
return address.trim();
}
catch (IOException e) { }
}
return address;
}
页:
[1]