package com.common;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import org.apache.log4j.Logger;
public class getMacAddress {
private Logger logger=Logger.getLogger(this.getClass());
public String getMacStr() throws Exception{
InetAddress ia = InetAddress.getLocalHost();
return getLocalMac(ia);
}
private static String getLocalMac(InetAddress ia) throws SocketException {
// TODO Auto-generated method stub
//获取网卡,获取地址
System.out.println(ia);
byte[] mac = NetworkInterface.getByInetAddress(ia).getHardwareAddress();
System.out.println(mac);
StringBuffer sb = new StringBuffer("");
for(int i=0; i<mac.length; i++) {
if(i!=0) {
sb.append("-");
}
//字节转换为整数
int temp = mac[i]&0xff;
String str = Integer.toHexString(temp);
if(str.length()==1) {
sb.append("0"+str);
}else {
sb.append(str);
}
}
return sb.toString().toUpperCase();
}
}
以上代码可在有网络的环境下正常使用,如果无网络,请参考
另一篇
jva 获取本机正在使用的网卡Mac地址
猜你喜欢
转载自blog.csdn.net/woai671/article/details/76153349
今日推荐
周排行