java获取电脑mac地址注意事项

2018-02-27 11:50:15来源:oschina作者:多金人点击

分享

/**


* 根据ip获取电脑mac地址


* @param ip


* @return 电脑mac地址


*/


public String getMACAddress(String ip) {


String macAddress = "";


Process p = null;


Runtime runtime = java.lang.Runtime.getRuntime();


try {


p = runtime.exec("nbtstat -A " + ip);


} catch (IOException e2) {


//这句话在windows电脑上正常,但是苹果电脑上会报错:java.io.IOException: Cannot run program "nbtstat": error=2, No such file or directory


log.error("FunMethod getMACAddress Runtime error============"+e2.getMessage());


}


if(p!=null){


InputStreamReader ir = new InputStreamReader(p.getInputStream());


LineNumberReader input = new LineNumberReader(ir);


for (int i = 1; i < 100; i++) {


String str = null;


try {


str = input.readLine();


} catch (IOException e) {


log.error("FunMethod getMACAddress readLine error============"+e.getMessage());


}


if (str != null) {


if (str.indexOf("MAC Address") > 1) {


macAddress = str.substring(str.indexOf("MAC Address") + 14, str.length());


break;


}


}


}


}

return macAddress;


}

最新文章

123

最新摄影

闪念基因

微信扫一扫

第七城市微信公众平台