zoukankan      html  css  js  c++  java
  • windows和linux获取本机ip

    InetAddress.getLocalHost().getHostAddress()
    获取本机ip在linux上报错: Caused by: java.net.UnknownHostException

    信息如下

    Caused by: java.net.UnknownHostException: host-xxx: Name or service not known
    	at java.net.Inet4AddressImpl.lookupAllHostAddr(Native Method)
    	at java.net.InetAddress$2.lookupAllHostAddr(InetAddress.java:929)
    	at java.net.InetAddress.getAddressesFromNameService(InetAddress.java:1324)
    	at java.net.InetAddress.getLocalHost(InetAddress.java:1501)
    	... 76 common frames omitted
    

    原因:上边的代码只能在windows获取ip,在linux上不行(linux解决方案也行,但是一般开发没运维那么大的权限啊!!!)

    java代码解决方案:

    package cxshyf.msjt.sign.utils;
    
    import java.net.InetAddress;
    import java.net.NetworkInterface;
    import java.net.SocketException;
    import java.net.UnknownHostException;
    import java.util.Enumeration;
    
    /**
     * 常用工具类
     */
    public class IPUtils {
    
        /**
         * 获取本地IP地址
         *
         * @throws SocketException
         */
        public static String getLocalIP() throws UnknownHostException {
            if (isWindowsOS()) {
                return InetAddress.getLocalHost().getHostAddress();
            } else {
                return getLinuxLocalIp();
            }
        }
    
        /**
         * 判断操作系统是否是Windows
         *
         * @return
         */
        public static boolean isWindowsOS() {
            boolean isWindowsOS = false;
            String osName = System.getProperty("os.name");
            if (osName.toLowerCase().indexOf("windows") > -1) {
                isWindowsOS = true;
            }
            return isWindowsOS;
        }
    
    
        /**
         * 获取Linux下的IP地址
         *
         * @return IP地址
         * @throws SocketException
         */
        private static String getLinuxLocalIp() {
            String ip = "";
            try {
                for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements(); ) {
                    NetworkInterface intf = en.nextElement();
                    String name = intf.getName();
                    if (!name.contains("docker") && !name.contains("lo")) {
                        for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements(); ) {
                            InetAddress inetAddress = enumIpAddr.nextElement();
                            if (!inetAddress.isLoopbackAddress()) {
                                String ipaddress = inetAddress.getHostAddress();
                                if (!ipaddress.contains("::") && !ipaddress.contains("0:0:") && !ipaddress.contains("fe80")) {
                                    ip = ipaddress;
                                    System.out.println(ipaddress);
                                }
                            }
                        }
                    }
                }
            } catch (SocketException ex) {
                System.out.println("获取ip地址异常");
                ip = "127.0.0.1";
                ex.printStackTrace();
            }
            return ip;
        }
    }
    

    参考:

  • 相关阅读:
    adb常用命令和工具
    playwright学习记录
    vue,element-ui表格,多个单元格值可修改(点击聚焦后变成input,失去焦点请求保存)
    vue,element-ui表格,合并单元格,如果需要合并的数据隔行,需要重新排列数组
    cas-5.3.x接入REST登录认证,移动端登录解决方案
    企业级cas5.3登录页面修改
    cas实现单点登录mysql,oracle双版本
    Mycat实现MySQL主从复制和读写分离(双主双从)
    IDEA安装插件后默认存放的位置
    值得推荐的Idea十几大优秀插件
  • 原文地址:https://www.cnblogs.com/linyufeng/p/13164600.html
Copyright © 2011-2022 走看看