cm-cloud/cloud-common/src/main/java/com/cm/common/utils/AddressUtil.java

30 lines
759 B
Java
Raw Normal View History

2019-07-27 23:03:27 +08:00
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();
}
}