// 通过IP获取网卡地址
private String getMacAddressIP(String remotePcIP) {
String str = "";
String macAddress = "";
try {
Process pp = Runtime.getRuntime().exec("netstat -a " + remotePcIP);
System.out.println("pp = " + pp);
InputStreamReader ir = new InputStreamReader(pp.getInputStream());
LineNumberReader input = new LineNumberReader(ir);
for (int i = 1; i < 100; i++) {
str = input.readLine();
//System.out.println("str = " + str);
if (str != null) {
if (str.indexOf("MAC Address") > 1) {
macAddress = str.substring(
str.indexOf("MAC Address") + 14, str.length());
break;
}
}
}
} catch (IOException ex) {
}
return macAddress;
}
private String getMacAddressIP(String remotePcIP) {
String str = "";
String macAddress = "";
try {
Process pp = Runtime.getRuntime().exec("netstat -a " + remotePcIP);
System.out.println("pp = " + pp);
InputStreamReader ir = new InputStreamReader(pp.getInputStream());
LineNumberReader input = new LineNumberReader(ir);
for (int i = 1; i < 100; i++) {
str = input.readLine();
//System.out.println("str = " + str);
if (str != null) {
if (str.indexOf("MAC Address") > 1) {
macAddress = str.substring(
str.indexOf("MAC Address") + 14, str.length());
break;
}
}
}
} catch (IOException ex) {
}
return macAddress;
}
if (str.indexOf("MAC Address") > 1) 为false,没有MAC Address字符串。
忘哪位高手帮我再改改,谢谢! ^_^
那可能跟笔者的版本不同。自己先在命令行下运行下 netstat -a 然后看看物理地址那行的信息是啥样的吧。记录下来,修改对应的“str.indexOf("MAC Address")”匹配条件就行了。
ifconfig
eth0 Link encap:Ethernet HWaddr 00:03:49:53:D3:16
inet addr:192.168.234.128 Bcast:192.168.234.255 Mask:255.255.255.0
inet6 addr: fe80::20c:29ff:fe33:daf6/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:27 errors:0 dropped:0 overruns:0 frame:0
TX packets:35 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:3162 (3.0 KiB) TX bytes:8630 (8.4 KiB)然后配合下过滤命令:
ifconfig | grep HWaddr用“Runtime”执行直接就得到你要的结果啦。
来唯一确定。(精度达到1/4秒)
之前用php获取的也是the server mac address。
头大了我……
不过话又说回来了,既然有这放的要求,就肯定有解答的。
有人说使用Ajax后台获取,也有人说使用asp获取,也有人说使用java获取,通过servlet传给前台jsp小脚本。nnd,这么多,没一个是我想要的。看到个方法,兴奋一下子。看个答案,测试下,兴奋剂就泄了。比阳痿还快…… -_- 现在一看到ie下的ActiveX组件就来火……
当然是Java代码所运行的地方,也就是Server端。
http://stackoverflow.com/questions/4525013/applet-not-able-to-access-networkinterface-gethardwareaddress-api
另外这里有用Applet操作NetworkInterface的样例代码:
https://forums.oracle.com/forums/thread.jspa?threadID=1144276
不过具体要怎么配置许可还需要研究。
{
InputStreamReader read = null;
LineNumberReader input = null;
try {
Process process = Runtime.getRuntime().exec("ipconfig -all");
read = new InputStreamReader(process.getInputStream());
input = new LineNumberReader(read);
int index = -1;
while(input.read() != index){
String readLing = input.readLine()+"";
if(readLing.contains("Physical Address")){
System.out.println("获取本机上的Mac地址:"+readLing);
}
}
} catch (Exception e) {
e.printStackTrace();
}finally{
if(input!=null){
input.close();
}