package com.lovo.util; import java.io.BufferedReader; import java.io.InputStream; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; public class PublicInterIp { /** * @param args * @throws Exception */ public String getPublicIp() { try { String path = "http://iframe.ip138.com/ic.asp";// 要获得html页面内容的地址 URL url = new URL(path);// 创建url对象 HttpURLConnection conn = (HttpURLConnection) url.openConnection();// 打开连接 conn.setRequestProperty("contentType", "GBK"); // 设置url中文参数编码 conn.setConnectTimeout(5 * 1000);// 请求的时间 conn.setRequestMethod("GET");// 请求方式 InputStream inStream = conn.getInputStream(); // readLesoSysXML(inStream); BufferedReader in = new BufferedReader(new InputStreamReader( inStream, "GBK")); StringBuffer buffer = new StringBuffer(); String line = ""; // 读取获取到内容的最后一行,写入 while ((line = in.readLine()) != null) { buffer.append(line); } String str = buffer.toString(); String ipString1 = str.substring(str.indexOf("[")); // 获取你的IP是中间的[182.149.82.50]内容 String ipsString2 = ipString1.substring(ipString1.indexOf("[") + 1, ipString1.lastIndexOf("]")); //获取当前IP地址所在地址 /* String ipsString3=ipString1.substring(ipString1.indexOf(": "),ipString1.lastIndexOf("</center>")); System.err.println(ipsString3);*/ // 返回公网IP值 return ipsString2; } catch (Exception e) { System.out.println("获取公网IP连接超时"); return "连接超时"; } } public static void main(String[] args) { PublicInterIp interIp=new PublicInterIp(); System.err.println(interIp.getPublicIp()); } }
java 获取公网(外网IP)很实用!,布布扣,bubuko.com
原文地址:http://blog.csdn.net/oqidai123456/article/details/36888453