1、获取本地ip

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 "";
}

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"读取失败";
}
}
