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;
}
}
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。