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

    参考:

  • 相关阅读:
    真正的成长就是颠覆你以前的世界观
    别蠢到用暴露自己软肋的方式,去表达你对别人的信任
    微信企业号的JAVA开发平台
    谁的青春不迷茫
    我才懒得去想我十年后是什么样子,我只在乎十年后的自己怎么看现在的我。
    2016年03月书单
    在人山人海里,你不必记得我
    Android模拟器Intel Atom下载安装配置
    Git与TortoiseGit使用方法
    Android开发环境搭建
  • 原文地址:https://www.cnblogs.com/linyufeng/p/13164600.html
Copyright © 2011-2022 走看看