• 一个获取当前登录机器IP的工具类


    一个获取当前请求机器IP的工具类

    代码如下

    os对象

    /**
     * @author wj
     * @date 2022.11.09 16:57
     */
    public class OS {
    
        private static final String OS_NAME = System.getProperty("os.name").toLowerCase();
        private static final OS instance = new OS();
        private EPlatform platform;
    
        private OS() {
        }
    
        public static boolean isLinux() {
            return OS_NAME.contains("linux");
        }
    
        public static boolean isMacOS() {
            return OS_NAME.contains("mac") && OS_NAME.indexOf("os") > 0 && !OS_NAME.contains("x");
        }
    
        public static boolean isMacOSX() {
            return OS_NAME.contains("mac") && OS_NAME.indexOf("os") > 0 && OS_NAME.indexOf("x") > 0;
        }
    
        public static boolean isWindows() {
            return OS_NAME.contains("windows");
        }
    
        public static boolean isOS2() {
            return OS_NAME.contains("os/2");
        }
    
        public static boolean isSolaris() {
            return OS_NAME.contains("solaris");
        }
    
        public static boolean isSunOS() {
            return OS_NAME.contains("sunos");
        }
    
        public static boolean isMPEiX() {
            return OS_NAME.contains("mpe/ix");
        }
    
        public static boolean isHPUX() {
            return OS_NAME.contains("hp-ux");
        }
    
        public static boolean isAix() {
            return OS_NAME.contains("aix");
        }
    
        public static boolean isOS390() {
            return OS_NAME.contains("os/390");
        }
    
        public static boolean isFreeBSD() {
            return OS_NAME.contains("freebsd");
        }
    
        public static boolean isIrix() {
            return OS_NAME.contains("irix");
        }
    
        public static boolean isDigitalUnix() {
            return OS_NAME.contains("digital") && OS_NAME.indexOf("unix") > 0;
        }
    
        public static boolean isNetWare() {
            return OS_NAME.contains("netware");
        }
    
        public static boolean isOSF1() {
            return OS_NAME.contains("osf1");
        }
    
        public static boolean isOpenVMS() {
            return OS_NAME.contains("openvms");
        }
    
        public static EPlatform getPlatform() {
            if (isAix()) {
                instance.platform = EPlatform.AIX;
            } else if (isDigitalUnix()) {
                instance.platform = EPlatform.DIGITAL_UNIX;
            } else if (isFreeBSD()) {
                instance.platform = EPlatform.FREE_BSD;
            } else if (isHPUX()) {
                instance.platform = EPlatform.HP_UX;
            } else if (isIrix()) {
                instance.platform = EPlatform.IRIX;
            } else if (isLinux()) {
                instance.platform = EPlatform.LINUX;
            } else if (isMacOS()) {
                instance.platform = EPlatform.MAC_OS;
            } else if (isMacOSX()) {
                instance.platform = EPlatform.MAC_OS_X;
            } else if (isMPEiX()) {
                instance.platform = EPlatform.MP_EI_X;
            } else if (isNetWare()) {
                instance.platform = EPlatform.NET_WARE_411;
            } else if (isOpenVMS()) {
                instance.platform = EPlatform.OPEN_VMS;
            } else if (isOS2()) {
                instance.platform = EPlatform.OS2;
            } else if (isOS390()) {
                instance.platform = EPlatform.OS390;
            } else if (isOSF1()) {
                instance.platform = EPlatform.OSF1;
            } else if (isSolaris()) {
                instance.platform = EPlatform.SOLARIS;
            } else if (isSunOS()) {
                instance.platform = EPlatform.SUN_OS;
            } else if (isWindows()) {
                instance.platform = EPlatform.WINDOWS;
            } else {
                instance.platform = EPlatform.OTHERS;
            }
    
            return instance.platform;
        }
    }
    
    
    • 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
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124

    定义枚举类

    public enum EPlatform {
    
        ANY("any"),
        LINUX("Linux"),
        MAC_OS("Mac OS"),
        MAC_OS_X("Mac OS X"),
        WINDOWS("Windows"),
        OS2("OS/2"),
        SOLARIS("Solaris"),
        SUN_OS("SunOS"),
        MP_EI_X("MPE/iX"),
        HP_UX("HP-UX"),
        AIX("AIX"),
        OS390("OS/390"),
        FREE_BSD("FreeBSD"),
        IRIX("Irix"),
        DIGITAL_UNIX("Digital Unix"),
        NET_WARE_411("NetWare"),
        OSF1("OSF1"),
        OPEN_VMS("OpenVMS"),
        OTHERS("Others");
    
        private String description;
    
        private EPlatform(String desc) {
            this.description = desc;
        }
    
        public String toString() {
            return this.description;
        }
    }
    
    
    • 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

    工具类代码如下

    /**
     * @author wj
     * @date 2022.11.09 16:55
     */
    @Slf4j
    public class NicTools {
    
        public NicTools() {
        }
    
        public static String getIp() {
            List ipList = getIPList();
            return ipList.isEmpty() ? "" : (String)ipList.get(0);
        }
    
        public static boolean isIp(String ip) {
            if (!StringUtils.isEmpty(ip) && ip.length() >= 7 && ip.length() <= 15) {
                String expression = "([1-9]|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])(\\.(\\d|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])){3}";
                Pattern pattern = Pattern.compile(expression);
                Matcher matcher = pattern.matcher(ip);
                return matcher.find();
            } else {
                return false;
            }
        }
    
        public static boolean isNotIp(String ip) {
            return !isIp(ip);
        }
    
        public static List getIPList() {
            return new ArrayList(parseIP().keySet());
        }
    
        public static Map parseIP() {
            Map map = new HashMap();
    
            try {
                Enumeration addresses;
                InetAddress address;
                if (OS.getPlatform() == EPlatform.WINDOWS) {
                    InetAddress candidateAddress = null;
                    Enumeration networkInterfaces = NetworkInterface.getNetworkInterfaces();
    
                    while(networkInterfaces.hasMoreElements()) {
                        NetworkInterface networkInterface = (NetworkInterface)networkInterfaces.nextElement();
                        addresses = networkInterface.getInetAddresses();
    
                        while(addresses.hasMoreElements()) {
                            address = (InetAddress)addresses.nextElement();
                            if (!address.isLoopbackAddress()) {
                                if (address.isSiteLocalAddress()) {
                                    map.put(address.getHostAddress(), address.getHostAddress());
                                } else {
                                    candidateAddress = address;
                                }
                            }
                        }
                    }
    
                    if (map.isEmpty()) {
                        assert candidateAddress != null;
    
                        map.put(candidateAddress.getHostAddress(), candidateAddress.getHostName());
                    }
                } else {
                    Enumeration interfaces = NetworkInterface.getNetworkInterfaces();
    
                    while(true) {
                        NetworkInterface networkInterface;
                        String network;
                        do {
                            if (!interfaces.hasMoreElements()) {
                                return map;
                            }
    
                            networkInterface = (NetworkInterface)interfaces.nextElement();
                            network = networkInterface.getName();
                        } while(!"e".equalsIgnoreCase(network.substring(0, 1)) && !"b".equalsIgnoreCase(network.substring(0, 1)));
    
                        addresses = networkInterface.getInetAddresses();
    
                        while(addresses.hasMoreElements()) {
                            address = (InetAddress)addresses.nextElement();
                            if (!(address instanceof Inet6Address)) {
                                map.put(address.getHostAddress(), address.getHostName());
                            }
                        }
                    }
                }
            } catch (SocketException var6) {
                log.error("Get Host Ip Address exception:", var6);
            }
            return map;
        }
    }
    
    
    • 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
    • 93
    • 94
    • 95
    • 96
    • 97

    测试示例

    创建一个spring boot应用,编写测试代码如下

    测试示例

    创建一个spring boot应用,编写测试代码如下

    在这里插入图片描述

    返回结果如下:
    在这里插入图片描述

  • 相关阅读:
    python生成器总结
    SpringCloud - Spring Cloud Netflix 之 Hystrix Dashboard仪表盘监控(九)
    [附源码]Python计算机毕业设计SSM绝味鸭脖连锁店信息系统(程序+LW)
    ELK 简介安装
    pytorch搭建squeezenet网络的整套工程(升级版)
    WSL 解决code .报‘code‘ not fount无法打开Vscode问题
    安卓温升thermal介绍
    【深度学习实验】前馈神经网络(final):自定义鸢尾花分类前馈神经网络模型并进行训练及评价
    【用Python画画】六一儿童节画爱心
    《工程伦理与学术道德》之《工程活动中的环境伦理》
  • 原文地址:https://blog.csdn.net/weixin_48161246/article/details/127809295