`

获取本地ip

    博客分类:
  • java
 
阅读更多
 /**
     * @Title: getLocalIp
     * @Description: 获取本地ip
     * @return
     */
    public static String getLocalIp() {

        Enumeration<NetworkInterface> networkInterface;

        try {
            networkInterface = NetworkInterface.getNetworkInterfaces();

        } catch (SocketException e) {
            throw new IllegalStateException(e);
        }
        String ip = null;
        while (networkInterface.hasMoreElements()) {

            NetworkInterface ni = networkInterface.nextElement();
            Enumeration<InetAddress> inetAddress = ni.getInetAddresses();
            while (inetAddress.hasMoreElements()) {

                InetAddress ia = inetAddress.nextElement();
                if (ia instanceof Inet6Address)
                    continue; // ignore ipv6
                if (!ia.isLoopbackAddress()
                        && ia.getHostAddress().indexOf(":") == -1 && "127.0.0.1".equals(ia.getHostAddress()) == false) {
                    ip = ia.getHostAddress();
                }
            }
        }
        if(null==ip || "".equals(ip)){
        	ip = "10.11.155.36";
        }
        return ip;
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics