1、获取本地ip
![在这里插入图片描述](https://img-blog.csdnimg.cn/20191223113808966.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzM3OTE3Njkx,size_16,color_FFFFFF,t_70)
2、获取本机的网络ip
public static String getIp(){
try {
Element element = Jsoup.connect("http://www.ip38.com/").get()
.select("a[href^=/ip.php?ip]").first();
if (element != null) {
return element.text();
}
} catch (Exception e) {
e.printStackTrace();
}
return "";
}
![在这里插入图片描述](https://img-blog.csdnimg.cn/20210303095124143.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzM3OTE3Njkx,size_16,color_FFFFFF,t_70)
3、根据ip获取所在地
public static String getAddressByIp(String ip){
try{
URL url=new URL("http://opendata.baidu.com/api.php?query="+ip+"&co=&resource_id=6006&t=1433920989928&ie=utf8&oe=utf-8&format=json");
URLConnection conn=url.openConnection();
BufferedReader reader=new BufferedReader(new InputStreamReader(conn.getInputStream(),"utf-8"));
String line=null;
StringBuffer result=new StringBuffer();
while((line=reader.readLine())!=null){
result.append(line);
}
reader.close();
JSONObject jsStr=JSON.parseObject(result.toString());
JSONArray jsData=(JSONArray)jsStr.get("data");
JSONObject data=(JSONObject)jsData.get(0);
return(String)data.get("location");
}catch(IOException e){
return"读取失败";
}
}
![在这里插入图片描述](https://img-blog.csdnimg.cn/20210303095248568.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzM3OTE3Njkx,size_16,color_FFFFFF,t_70)