/**
* windows 7 专用 获取MAC地址
*
* @return
* @throws Exception
*/
public static String getWindows7MACAddress() {
StringBuffer sb = new StringBuffer();
try {
// 获取本地IP对象
InetAddress ia = InetAddress.getLocalHost();
// 获得网络接口对象(即网卡),并得到mac地址,mac地址存在于一个byte数组中。
byte[] mac = NetworkInterface.getByInetAddress(ia).getHardwareAddress();
// 下面代码是把mac地址拼装成String
for (int i = 0; i < mac.length; i++) {
// mac[i] & 0xFF 是为了把byte转化为正整数
String s = Integer.toHexString(mac[i] & 0xFF);
sb.append(s.length() == 1 ? 0 + s : s);
}
}
catch (Exception ex) {
System.out.println("windows 7方式未获取到网卡地址");
}
return sb.toString();
}
/**
* 获取MAC地址
*
* @param argc
* 运行参数.
* @throws Exception
*/
public static String getMACAddress() {
// windows
String mac = getWindowsMACAddress();
// windows7
if (isNull(mac)) {
mac = getWindows7MACAddress();
}
// unix
if (isNull(mac)) {
mac = getUnixMACAddress();
}
if (!isNull(mac)) {
mac = mac.replace("-", "");
}
else {
mac = "ABCDEFGHIJ";
}
return mac.toLowerCase();
}
public static boolean isNull(Object strData) {
if (strData == null || String.valueOf(strData).trim().equals("")) {
return true;
}
return false;
}
public static void main(String[] args) {
System.out.println(getWindows7MACAddress());
}
}