一道java的题目,急求答案

2025-01-07 18:50:49
推荐回答(1个)
回答1:

public class ListIP {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("本机的外网IP是:"
+ ListIP.getWebIp("http://www.ip138.com/ip2city.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);

}

br.close();
webContent = sb.toString();
int start = webContent.indexOf("[") + 1;
int end = webContent.indexOf("]");
webContent = webContent.substring(start, end);

return webContent;

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

}
}

}