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;
        }
    }
    

    参考:

  • 相关阅读:
    BZOJ3236:[AHOI2013]作业(莫队,分块)
    BZOJ5334:[TJOI2018]数学计算(线段树)
    BZOJ3173:[TJOI2013]最长上升子序列(Splay)
    BZOJ3211:花神游历各国(线段树)
    BZOJ3155:Preprefix sum(线段树)
    HDU5002:Tree(LCT)
    【BZOJ 1911】 [Apio2010]特别行动队
    【BZOJ 2875】 [Noi2012]随机数生成器
    【BZOJ 1054】 [HAOI2008]移动玩具
    【BZOJ 1497】 [NOI2006]最大获利
  • 原文地址:https://www.cnblogs.com/linyufeng/p/13164600.html
Copyright © 2011-2022 走看看