java获取本机外网IP
2014-04-11 18:44
309 查看
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;
}
}
}
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地址的方法
- java获取本机的外网ip
- Java获取本机外网IP及区域信息
- Java获取外网ip地址
- java获取本地局域网的外网IP 笔记
- Java获取本机外网ip地址的方法
- java获取本机外网ip
- java通过外网ip获取本地城市地理位置
- Java获取本机外网ip地址的方法
- java获取本地局域网的外网IP ....
- java代码实现外网IP的获取
- Java获取外网IP
- java获取外网IP
- java获取服务器真实外网IP
- java 获取本地的或者外网的IP
- Java 获取本机局域网的外网IP
- Linux环境下java后台获取外网IP
- java 获取公网(外网IP)很实用!
- JAVA 获取当前 内网 和 外网 的IP 地址
- JAVA 获取当前 内网 和 外网 的IP 地址