Java web 怎么得到客户端的 Mac 地址
1个回答
展开全部
参考代码如下:
String[] cmd;
if (OS.contains("win")) {
// 对于windows
macpt = Pattern
.compile("[0-9a-f]+-[0-9a-f]+-[0-9a-f]+-[0-9a-f]+-[0-9a-f]+-[0-9a-f]+");
String[] a = { "arp", "-a", ip };
cmd = a;
} else {
// 对于mac 和 linux
macpt = Pattern
.compile("[0-9a-f]+:[0-9a-f]+:[0-9a-f]+:[0-9a-f]+:[0-9a-f]+:[0-9a-f]+");
String[] a = { "arp", ip };
cmd = a;
}
try {
// 执行命令行代码
Process p = Runtime.getRuntime().exec(cmd);
p.waitFor();
// 读取buffer
BufferedReader reader = new BufferedReader(new InputStreamReader(
p.getInputStream()));
String line = reader.readLine();
while (line != null) {
Matcher m = macpt.matcher(line);
if (m.find()) {
System.out.println("Found");
System.out.println("MAC: " + m.group(0));
return m.group(0);
}
line = reader.readLine();
}
} catch (IOException e1) {
e1.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
return "";
}
String[] cmd;
if (OS.contains("win")) {
// 对于windows
macpt = Pattern
.compile("[0-9a-f]+-[0-9a-f]+-[0-9a-f]+-[0-9a-f]+-[0-9a-f]+-[0-9a-f]+");
String[] a = { "arp", "-a", ip };
cmd = a;
} else {
// 对于mac 和 linux
macpt = Pattern
.compile("[0-9a-f]+:[0-9a-f]+:[0-9a-f]+:[0-9a-f]+:[0-9a-f]+:[0-9a-f]+");
String[] a = { "arp", ip };
cmd = a;
}
try {
// 执行命令行代码
Process p = Runtime.getRuntime().exec(cmd);
p.waitFor();
// 读取buffer
BufferedReader reader = new BufferedReader(new InputStreamReader(
p.getInputStream()));
String line = reader.readLine();
while (line != null) {
Matcher m = macpt.matcher(line);
if (m.find()) {
System.out.println("Found");
System.out.println("MAC: " + m.group(0));
return m.group(0);
}
line = reader.readLine();
}
} catch (IOException e1) {
e1.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
return "";
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询