Java IP Address Get getLocalIPs(boolean refresh)

Here you can find the source of getLocalIPs(boolean refresh)

Description

get Local I Ps

License

Open Source License

Declaration

public static List<String> getLocalIPs(boolean refresh) 

Method Source Code

//package com.java2s;
/**/*from  w w w .  j  ava 2s.com*/
 *
 * Copyright (c) 2014, the Railo Company Ltd. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either 
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public 
 * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
 * 
 **/

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;

public class Main {
    private static boolean isCacheEnabled = false;
    private static boolean isCacheValid = false;
    private static List<String> cachedLocalIPs = null;

    public static List<String> getLocalIPs(boolean refresh) {

        if (isCacheEnabled && isCacheValid && !refresh) {

            return new ArrayList<String>(cachedLocalIPs);
        }

        List<String> result = new ArrayList();

        try {

            Enumeration<NetworkInterface> eNics = NetworkInterface.getNetworkInterfaces();

            while (eNics.hasMoreElements()) {

                NetworkInterface nic = eNics.nextElement();

                if (nic.isUp()) {

                    Enumeration<InetAddress> eAddr = nic.getInetAddresses();

                    while (eAddr.hasMoreElements()) {

                        InetAddress inaddr = eAddr.nextElement();

                        String addr = inaddr.toString();

                        if (addr.startsWith("/"))
                            addr = addr.substring(1);

                        if (addr.indexOf('%') > -1)
                            addr = addr.substring(0, addr.indexOf('%')); // internal zone in some IPv6; http://en.wikipedia.org/wiki/IPv6_Addresses#Link-local%5Faddresses%5Fand%5Fzone%5Findices

                        result.add(addr);
                    }
                }
            }
        } catch (SocketException e) {

            result.add("127.0.0.1");
            result.add("0:0:0:0:0:0:0:1");
        }

        if (isCacheEnabled) {

            cachedLocalIPs = result;
            isCacheValid = true;
        }

        return result;
    }
}

Related

  1. getLocalIPCollection()
  2. getLocalIPList()
  3. getLocalIps()
  4. getLocalIPs()
  5. getLocalIPs()
  6. getLocalIpv4()
  7. getLocalIPv6Address()
  8. getLocalNetWorkIp()
  9. getLocalV4Ip()