jsp如何获取客户端的mac地址

时间:2021-04-16

  下面介绍jsp如何获取客户端的mac地址。

import java.io.IOException;
import java.io.InputStreamReader;
import java.io.LineNumberReader;
public class GetMACAddress {
public String getMACAddress(String ipAddress) {
String str = "", strMAC = "", macAddress = "";
try {
Process pp = Runtime.getRuntime().exec("nbtstat -a " + ipAddress);
InputStreamReader ir = new InputStreamReader(pp.getInputStream());
LineNumberReader input = new LineNumberReader(ir);
for (int i = 1; i < 100; i++) {
str = input.readLine();
if (str != null) {
if (str.indexOf("MAC Address") > 1) {
strMAC = str.substring(str.indexOf("MAC Address") + 14,
str.length());
break;
}
}
}
} catch (IOException ex) {
return "Can't Get MAC Address!";
}
//
if (strMAC.length() < 17) {
return "Error!";
}
macAddress = strMAC.substring(0, 2) + ":" + strMAC.substring(3, 5)
+ ":" + strMAC.substring(6, 8) + ":" + strMAC.substring(9, 11)
+ ":" + strMAC.substring(12, 14) + ":"
+ strMAC.substring(15, 17);
//
return macAddress;
}
public static void main(String[] args) {
GetMACAddress getMACAddress = new GetMACAddress();
System.out.println(getMACAddress.getMACAddress("59.78.63.38")); //获得该ip地址的mac地址
}
public static String procAll(String str) {
return procStringEnd(procFirstMac(procAddress(str)));
}
public static String procAddress(String str) {
int indexof = str.indexOf("Physical Address");
if (indexof > 0) {
return str.substring(indexof, str.length());
}
return str;
}
public static String procFirstMac(String str) {
int indexof = str.indexOf(":");
if (indexof > 0) {
return str.substring(indexof + 1, str.length()).trim();
}
return str;
}
public static String procStringEnd(String str) {
int indexof = str.indexOf("/r");
if (indexof > 0) {
return str.substring(0, indexof).trim();
}
return str;
}
}

  只要写一个servlet来调用这个类的getMACAddress(String netip)方法就可以获得客户端的mac地址了,相信你会写jsp应该对servlet也不陌生吧,在jsp中调用servlet通过session传递返回的mac地址,加以判断就可以了。

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章