Przeglądaj źródła

mac地址获取方式

danchaofan1412 5 lat temu
rodzic
commit
ef69e57d68

+ 40 - 0
framework-boot/src/main/java/com/mrxu/framework/boot/util/SystemInfo.java

@@ -0,0 +1,40 @@
+package com.mrxu.framework.boot.util;
+
+import com.mrxu.framework.boot.entity.BusinessException;
+
+import java.net.InetAddress;
+import java.net.NetworkInterface;
+
+public class SystemInfo {
+
+    private static String getLocalMac()  {
+        StringBuffer sb = new StringBuffer("");
+        try {
+            InetAddress ia =  InetAddress.getLocalHost();
+            //获取网卡,获取地址
+            byte[] mac = NetworkInterface.getByInetAddress(ia).getHardwareAddress();
+            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);
+                }
+            }
+        } catch (Exception e) {
+            throw new BusinessException("获取mac异常"+e.getMessage());
+        }
+
+        return sb.toString().toUpperCase();
+    }
+
+    public static void main(String[] args) {
+        System.out.println(getLocalMac());
+    }
+}