jiangping
2025-04-21 c92ef51676efa03d41c1cf7b94f4077d58a7babd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package com.doumee.core.utils;
 
import lombok.extern.slf4j.Slf4j;
 
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;
 
/**
 * 服务端工具类
 * @author  dm
 * @since 2025/03/31 16:44
 */
@Slf4j
public class Server {
 
    /**
     * 获取IP地址
     */
    public String getIP() {
        String ip = "获取失败";
        InetAddress inetAddress = getInetAddress();
        if (inetAddress != null) {
            ip = inetAddress.getHostAddress();
        }
        return ip;
    }
 
    /**
     * 获取MAC地址
     */
    public String getMAC() {
        try {
            InetAddress inetAddress = getInetAddress();
            if (inetAddress == null) {
                return "获取失败";
            }
            byte[] bs = NetworkInterface.getByInetAddress(inetAddress).getHardwareAddress();
            StringBuffer sb = new StringBuffer();
            for (int i = 0; i < bs.length; i++) {
                if (i != 0) {
                    sb.append(":");
                }
                // 字节转换为整数
                int temp = bs[i] & 0xff;
                // 把无符号整数参数所表示的值转换成以十六进制表示的字符串
                String str = Integer.toHexString(temp);
                if (str.length() == 1) {
                    sb.append("0" + str);
                } else {
                    sb.append(str);
                }
            }
            return sb.toString();
        } catch (Exception e) {
            return "获取失败";
        }
    }
 
    /**
     * 获取InetAddress对象
     */
    private InetAddress getInetAddress () {
        InetAddress inetAddress = null;
        try {
            for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements(); ) {
                NetworkInterface intf = en.nextElement();
                String name = intf.getName();
                if (name.contains("docker") || name.contains("lo")) {
                    return inetAddress;
                }
                for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements(); ) {
                    InetAddress ia = enumIpAddr.nextElement();
                    if (ia.isLoopbackAddress()) {
                        continue;
                    }
                    String address = ia.getHostAddress();
                    if (address.contains("::") || address.contains("0:0:") || address.contains("fe80")) {
                        continue;
                    }
                    if (!"127.0.0.1".equals(address)) {
                        inetAddress = ia;
                    }
                }
            }
        } catch (SocketException e) {
            log.error(e.getMessage(), e);
        }
        return inetAddress;
    }
}