win.hupubao.common.utils.MacUtils Maven / Gradle / Ivy
package win.hupubao.common.utils;
import java.net.InetAddress;
import java.net.NetworkInterface;
public class MacUtils {
public static String getLocalMac() {
InetAddress ia;
byte[] mac = null;
try {
//获取本地IP对象
ia = InetAddress.getLocalHost();
//获得网络接口对象(即网卡),并得到mac地址,mac地址存在于一个byte数组中。
mac = NetworkInterface.getByInetAddress(ia).getHardwareAddress();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
StringBuffer sb = new StringBuffer();
for (int i = 0; i < mac.length; i++) {
if (i != 0) {
sb.append("-");
}
//mac[i] & 0xFF 是为了把byte转化为正整数
String s = Integer.toHexString(mac[i] & 0xFF);
sb.append(s.length() == 1 ? 0 + s : s);
}
return sb.toString().toUpperCase();
}
public static void main(String[] args) {
System.out.println(getLocalMac());
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy