您的位置:首页 > 编程语言 > Java开发

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;

}

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: