java 获取ip地址 公网,Java获取当前电脑公网IP地址

思路:利用 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("\\

(.*?)\\");

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);

}

结果顺利获取

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值