Java IP Address Get getLocalHostIps()

Here you can find the source of getLocalHostIps()

Description

get Local Host Ips

License

Apache License

Declaration

public static String getLocalHostIps() 

Method Source Code

//package com.java2s;
/*//from  www  .j  a va  2s. c  o  m
 * BJAF - Beetle J2EE Application Framework
 * ???J2EE???????????
 * ??????2003-2015 ??? (www.beetlesoft.net)
 * 
 * ??????????????????
 *<http://www.apache.org/licenses/LICENSE-2.0>
 *????????????????????????
 *
 * ???????????????????????????????
 * ??? <yuhaodong@gmail.com/>.
 */

import java.net.InetAddress;
import java.net.NetworkInterface;

import java.util.Enumeration;

public class Main {

    public static String getLocalHostIps() {
        StringBuffer sb = new StringBuffer();
        final char flag = 2;
        try {
            Enumeration<?> netInterfaces = NetworkInterface.getNetworkInterfaces();
            while (netInterfaces.hasMoreElements()) {
                NetworkInterface ni = (NetworkInterface) netInterfaces.nextElement();
                Enumeration<InetAddress> ips = ni.getInetAddresses();
                while (ips.hasMoreElements()) {
                    InetAddress inetAddress = ips.nextElement();
                    String ip = inetAddress.getHostAddress();
                    if (!inetAddress.isLoopbackAddress() && ip.indexOf(":") == -1) {
                        sb.append(ip).append(flag);
                    }
                }
            }
        } catch (Exception e) {
            return "";
        }
        return sb.toString();
    }
}

Related

  1. getLocalHostIP()
  2. getLocalHostIp()
  3. getLocalhostIp()
  4. getLocalHostIp()
  5. getLocalHostIPAdress()
  6. getLocalHostIps()
  7. getLocalIp()
  8. getLocalIp()
  9. getLocalIP()