Java 如何获得本机IP 或者 公网IP
2024-08-02 22:17:29 218人阅读
public static void main(String[] args) throws Exception {
//
URL url = new URL("http://www.ip138.com/ip2city.asp");
URLConnection conn = url.openConnection();
conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.15) Gecko/20110303 Firefox/3.6.15");
conn.setRequestProperty("Content-Type", "text/html");
conn.setRequestProperty("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
InputStream is = conn.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is, "GB2312"));
String line = null;
while ((line = br.readLine()) != null) {
if (line.contains("您的IP地址是")) {
// System.out.println(line);
int start = line.indexOf(‘[‘) + 1;
int end = line.indexOf(‘]‘);
System.out.println(line.substring(start, end));
}
}
br.close();
}
Java 如何获得本机IP 或者 公网IP
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉:
投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。