思路:利用 IP地址查询的网站 获取当前电脑的公网IP地址
获取公网IP的几个方法(提供多个,实现错误重试)
// 方法1
private String getNowIP1() throws IOException {
String ip = null;
String chinaz = "http://ip.chinaz.com";
StringBuilder inputLine = new StringBuilder();
String read = "";
URL url = null;
HttpURLConnection urlConnection = null;
BufferedReader in = null;
try {
url = new URL(chinaz);
urlConnection = (HttpURLConnection) url.openConnection();
in = new BufferedReader(new InputStreamReader(urlConnection.getInputStream(), "UTF-8"));
while ((read = in.readLine()) != null) {
inputLine.append(read + "\r\n");
}
Pattern p = Pattern.compile("\\<dd class\\=\"fz24\">(.*?)\\<\\/dd>");
Matcher m = p.matcher(inputLine.toString());
if (m.find()) {
String ipstr = m.group(1);
ip = ipstr;
}
} finally {
if (in != null) {
in.close();
}
}
if (StringUtils.isEmpty(ip)) {
throw new RuntimeException();
}
return ip;
}
// 方法2
private String getNowIP2() throws IOException {
String ip = null;
BufferedReader br = null;
try {
URL url = new URL("https://v6r.ipip.net/?format=callback");
br = new BufferedReader(new InputStreamReader(url.openStream()));
String s = "";
StringBuffer sb = new StringBuffer("");
String webContent = "";
while ((s = br.readLine()) != null) {
sb.append(s + "\r\n");
}
webContent = sb.toString();
int start = webContent.indexOf("(") + 2;
int end = webContent.indexOf(")") - 1;
webContent = webContent.substring(start, end);
ip = webContent;
} finally {
if (br != null)
br.close();
}
if (StringUtils.isEmpty(ip)) {
throw new RuntimeException();
}
return ip;
}
// 方法3
private String getNowIP3() throws IOException {
String ip = null;
String objWebURL = "https://ip.900cha.com/";
BufferedReader br = null;
try {
URL url = new URL(objWebURL);
br = new BufferedReader(new InputStreamReader(url.openStream()));
String s = "";
String webContent = "";
while ((s = br.readLine()) != null) {
if (s.indexOf("我的IP:") != -1) {
ip = s.substring(s.indexOf(":") + 1);
break;
}
}
} finally {
if (br != null)
br.close();
}
if (StringUtils.isEmpty(ip)) {
throw new RuntimeException();
}
return ip;
}
// 方法4
private String getNowIP4() throws IOException {
String ip = null;
String objWebURL = "https://bajiu.cn/ip/";
BufferedReader br = null;
try {
URL url = new URL(objWebURL);
br = new BufferedReader(new InputStreamReader(url.openStream()));
String s = "";
String webContent = "";
while ((s = br.readLine()) != null) {
if (s.indexOf("互联网IP") != -1) {
ip = s.substring(s.indexOf("'") + 1, s.lastIndexOf("'"));
break;
}
}
} finally {
if (br != null)
br.close();
}
if (StringUtils.isEmpty(ip)) {
throw new RuntimeException();
}
return ip;
}
综合方法
public String getPublicIP() {
String ip = null;
// 第一种方式
try {
ip = this.getNowIP1();
ip.trim();
} catch (Exception e) {
System.out.println("getPublicIP - getNowIP1 failed ~ ");
}
if (!StringUtils.isEmpty(ip))
return ip;
// 第二种方式
try {
ip = this.getNowIP2();
ip.trim();
} catch (Exception e) {
System.out.println("getPublicIP - getNowIP2 failed ~ ");
}
if (!StringUtils.isEmpty(ip))
return ip;
// 第三种方式
try {
ip = this.getNowIP3();
ip.trim();
} catch (Exception e) {
System.out.println("getPublicIP - getNowIP3 failed ~ ");
}
if (!StringUtils.isEmpty(ip))
return ip;
// 第四种方式
try {
ip = this.getNowIP4();
ip.trim();
} catch (Exception e) {
System.out.println("getPublicIP - getNowIP4 failed ~ ");
}
if (!StringUtils.isEmpty(ip))
return ip;
return ip;
}
测试
public static void main(String[] args) {
ConcurrentPublicIP thisObj = new ConcurrentPublicIP();
String publicIP = thisObj.getPublicIP();
System.out.println(publicIP);
}
结果顺利获取