获取本机外网ip地址
2015-08-19 17:24
465 查看
package com.ning;
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"));
//+ Listip.getWebIp("http://www.ip138.com"));
+ Listip.getWebIp("http://1111.ip138.com/ic.asp"));
}
public static String getWebIp(String strUrl) {
try {
URL url = new URL(strUrl);
BufferedReader br = new BufferedReader(new InputStreamReader(url.openStream(),"GB2312"));
String s = "";
StringBuffer sb = new StringBuffer("");
String webContent = "";
while ((s = br.readLine()) != null) {
sb.append(s + "rn");
}
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;
}
}
}
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"));
//+ Listip.getWebIp("http://www.ip138.com"));
+ Listip.getWebIp("http://1111.ip138.com/ic.asp"));
}
public static String getWebIp(String strUrl) {
try {
URL url = new URL(strUrl);
BufferedReader br = new BufferedReader(new InputStreamReader(url.openStream(),"GB2312"));
String s = "";
StringBuffer sb = new StringBuffer("");
String webContent = "";
while ((s = br.readLine()) != null) {
sb.append(s + "rn");
}
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;
}
}
}
相关文章推荐
- 求斐波那契数列第n项,n<30,斐波那契数列前10项为 1,1,2,3,5,8,13,21,34,55
- smarty基础之五配置和两方法
- Mac 中安装 Apache Ant
- 全排序算法
- android获取设备唯一标识device_token
- java代码--文件过滤器
- 输入url发生了什么
- MySQL在创建索引之前一定要想到的事情
- 替换空格
- Python Video tutorial
- 深入浅出 RPC-(2)
- 问题:OAuth2.0;结果:帮你深入理解OAuth2.0协议
- Python游戏服务器开发日记(二)绕过GIL启动多线程Python环境
- Android反编译教程
- MySQLDump在使用之前一定要想到的事情
- 树莓派2在U8300W平台上用pppd拨号时出现“pppd: The remote system is required to authenticate itself”
- Introduction to Java Programming编程题3.22<判断点是否在矩形内>
- java 垃圾回收总结(1)
- Spring Side3的安全框架
- java中使用akka手记三 cluster详例