30 lines
759 B
Java
30 lines
759 B
Java
package com.cm.common.utils;
|
|
|
|
import java.net.InetAddress;
|
|
import java.net.NetworkInterface;
|
|
|
|
public class AddressUtil {
|
|
|
|
/**
|
|
* 获取MAC地址
|
|
*
|
|
* @return
|
|
* @throws Exception
|
|
*/
|
|
public static final String getMacAddress() throws Exception {
|
|
InetAddress inetAddress = InetAddress.getLocalHost();
|
|
byte[] mac = NetworkInterface.getByInetAddress(inetAddress).getHardwareAddress();
|
|
StringBuilder macAddress = new StringBuilder();
|
|
for (byte macByte : mac) {
|
|
if (macAddress.length() > 0) {
|
|
macAddress.append("-");
|
|
}
|
|
int macInt = macByte & 0xff;
|
|
String str = Integer.toHexString(macInt);
|
|
macAddress.append(str.length() == 1 ? 0 + str : str);
|
|
}
|
|
return macAddress.toString().toUpperCase();
|
|
}
|
|
|
|
}
|