Java IP Address Get getPublicIP()

Here you can find the source of getPublicIP()

Description

get Public IP

License

Apache License

Declaration

public static String getPublicIP() 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright 2012 I3M-GRyCAP//from  w  ww  .ja  v  a 2  s.  co  m
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ******************************************************************************/

import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;

public class Main {
    public static String getPublicIP() {
        String ip = "127.0.0.1";
        try {
            for (Enumeration<NetworkInterface> ifaces = NetworkInterface.getNetworkInterfaces(); ifaces
                    .hasMoreElements();) {
                NetworkInterface iface = ifaces.nextElement();
                for (Enumeration<InetAddress> addresses = iface.getInetAddresses(); addresses.hasMoreElements();) {
                    InetAddress address = addresses.nextElement();

                    ip = address.getHostAddress();

                    /**
                     * Checks for IPV4 address since Apache commons FTP does not appear to support IPV6 (as of early 2012)
                     */
                    if (address instanceof Inet4Address && !ip.startsWith("192") && !ip.startsWith("127")
                            && !ip.startsWith("0"))
                        return ip;

                }
            }
        } catch (SocketException e) {
            e.printStackTrace();
        }
        return ip;
    }
}

Related

  1. getLocalV4IpList()
  2. getLongIp(byte[] buff)
  3. getLongIp(String ipString)
  4. getPublicIP()
  5. getPublicIP()
  6. getPublicIP()
  7. getPublicIP()
  8. getPublicIP()
  9. getPublicIP2()