Here you can find the source of getPublicIP()
public static final String getPublicIP()
//package com.java2s; //License from project: Open Source License import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.MalformedURLException; import java.net.URL; public class Main { private final static java.util.regex.Pattern pattern = java.util.regex.Pattern .compile("^([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\." + "([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\." + "([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\." + "([01]?\\d\\d?|2[0-4]\\d|25[0-5])$"); public static final String getPublicIP() { URL url = null;/*from w ww . ja va2 s. c om*/ final String[] websites = new String[] { "http://checkip.amazonaws.com", "http://myexternalip.com/raw", "https://wtfismyip.com/text", "https://api.ipify.org/" }; for (String website : websites) { try { url = new URL(website); } catch (MalformedURLException e) { continue; } try (BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()))) { String line = in.readLine(); if (isIPValid(line)) return line; } catch (IOException e) { continue; } } return null; } public static boolean isIPValid(final String ipv4) { java.util.regex.Matcher matcher = pattern.matcher(ipv4); return matcher.matches(); } }