public static String getSelfIp() {
try {
URL url = new URL("http://pv.sohu.com/cityjson");
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("{");
int end = webContent.indexOf("}") + 1;
webContent = webContent.substring(start,end);
JSONObject obj = JSON.parseObject(webContent);
return obj.getString("cip");
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
java获取本机ip地址
最新推荐文章于 2024-07-08 10:10:12 发布