首页 > 编程语言 > 详细

java获取本机外网IP

时间:2014-04-12 08:43:11      阅读:379      评论:0      收藏:0      [点我收藏+]
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;


public class Listip {


public static void main(String[] args) throws Exception {
System.out.println("本机的外网IP是:"
+ Listip.getWebIp("http://iframe.ip138.com/ic.asp"));
}


public static String getWebIp(String strUrl) {
try {


URL url = new URL(strUrl);
BufferedReader 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");
}


br.close();
webContent = sb.toString();
int start = webContent.indexOf("[") + 1;
int end = webContent.indexOf("]");
System.out.println("webContent=" + webContent);
System.out.println("start=" + start);
System.out.println("end=" + end);
if (start < 0 || end < 0) {
return null;
}
webContent = webContent.substring(start, end);
return webContent;


} catch (Exception e) {
e.printStackTrace();
return "error open url:" + strUrl;


}
}
}

java获取本机外网IP,布布扣,bubuko.com

java获取本机外网IP

原文:http://blog.csdn.net/lky5387/article/details/23466917

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!