• linux环境下获取真实的ip地址


    package inas.test;
    
    import java.net.Inet6Address;
    import java.net.InetAddress;
    import java.net.NetworkInterface;
    import java.net.SocketException;
    import java.util.Enumeration;
    
    /**
     * IP工具类
     * @author lyh
     * @version 2012-7-5
     * @see IpGetter
     * @since
     */
    public class IpGetter
    {
        /**
         * 单网卡名称
         */
        private static final String NETWORK_CARD = "eth0";
    
        /**
         * 绑定网卡名称
         */
        private static final String NETWORK_CARD_BAND = "bond0";
    
        /**
         * 
         * Description: 得到本机名<br>
         * @return 
         * @see
         */
        public static String getLocalHostName()
        {
            try
            {
                InetAddress addr = InetAddress.getLocalHost();
                return addr.getHostName();
            }
            catch (Exception e)
            {
                System.out.println("IpGetter.getLocalHostName出现异常!异常信息:" + e.getMessage());
                return "";
            }
        }
    
        /**
         * Description: linux下获得本机IPv4 IP<br>
         * @return 
         * @see
         */
        public static String getLocalIP()
        {
            String ip = "";
            try
            {
                Enumeration<NetworkInterface> e1 = (Enumeration<NetworkInterface>)NetworkInterface.getNetworkInterfaces();
                while (e1.hasMoreElements())
                {
                    NetworkInterface ni = e1.nextElement();
    
                    //单网卡或者绑定双网卡
                    if ((NETWORK_CARD.equals(ni.getName()))
                        || (NETWORK_CARD_BAND.equals(ni.getName())))
                    {
                        Enumeration<InetAddress> e2 = ni.getInetAddresses();
                        while (e2.hasMoreElements())
                        {
                            InetAddress ia = e2.nextElement();
                            if (ia instanceof Inet6Address)
                            {
                                continue;
                            }
                            ip = ia.getHostAddress();
                        }
                        break;
                    }
                    else
                    {
                        continue;
                    }
                }
            }
            catch (SocketException e)
            {
                System.out.println("IpGetter.getLocalIP出现异常!异常信息:" + e.getMessage());
            }
            return ip;
        }
        public static void main(String[] args) {
            IpGetter ipGetter =new IpGetter();
            String str = IpGetter.getLocalIP();
            System.out.println("ip是:"+str);
        }
    
    }
  • 相关阅读:
    谈谈关于PHP连接数据库的两种方法(PDO&Mysqli)
    关于Mui严格模式下的报错解决方案
    CSS常用选择器的认识
    HTML汇总以及CSS的一些开端
    LAMP架构之PHP-FPM 服务器 转
    httpd配置
    notapai++ 使用小技巧
    转载-notepad++ zend-coding使用
    Apache下的配置文件httpd.conf、httpd-vhosts.conf 转
    Apache本机不同端口多站点配置:httpd-vhosts.conf(转载)
  • 原文地址:https://www.cnblogs.com/kunpengit/p/3302969.html
Copyright © 2020-2023  润新知