public class GetHostIpAddress {
/**
* @Description
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out
.println("本机的外网IP是:"
+ GetHostIpAddress
.getWebIp("http://www.ip138.com/ip2city.asp"));
}
/**
*
* @Description 根据访问的url得到路由的ip地址
* @param strUrl
* @return
*/
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("]");
webContent = webContent.substring(start, end);
return webContent;
} catch (Exception e) {
e.printStackTrace();
return "error open url:" + strUrl;
}
}
}
java获取路由ip地址
最新推荐文章于 2024-06-17 18:09:23 发布