【转载】 java根据ip地址获取详细地域信息

互联网有很多接口可以实现通过ip查询到具体的位置,如下:

通过淘宝IP地址库获取IP位置

1. 请求接口(GET):http://ip.taobao.com/service/getIpInfo.php?ip=[ip地址字串]
2. 响应信息:(json格式的)国家 、省(自治区或直辖市)、市(县)、运营商
3. 返回数据格式:
{"code":0,"data":{"ip":"210.75.225.254","country":"\u4e2d\u56fd","area":"\u534e\u5317",
"region":"\u5317\u4eac\u5e02","city":"\u5317\u4eac\u5e02","county":"","isp":"\u7535\u4fe1",
"country_id":"86","area_id":"100000","region_id":"110000","city_id":"110000",
"county_id":"-1","isp_id":"100017"}}
其中code的值的含义为,0:成功,1:失败。 

新浪的接口 :http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=json&ip=218.192.3.42
返回值
var remote_ip_info = {"ret":1,"start":"218.192.0.0","end":"218.192.7.255","country":"\u4e2d\u56fd","province":"\u5e7f\u4e1c","city":"\u5e7f\u5dde","district":"","isp":"\u6559\u80b2\u7f51","type":"\u5b66\u6821","desc":"\u5e7f\u5dde\u5927\u5b66\u7eba\u7ec7\u670d\u88c5\u5b66\u9662"};
通过jqry 获取相应的数据
$.getScript('数据接口',function(){
//新浪:remote_ip_info.country
}) 

腾讯IP分享计划的地址获取IP所在地:

Java调用淘宝ip查询接口查询地域的一个java实例:

[java] view plain copy 在CODE上查看代码片 派生到我的代码片
  1. import java.io.BufferedReader;  
  2. import java.io.DataOutputStream;  
  3. import java.io.IOException;  
  4. import java.io.InputStreamReader;  
  5. import java.io.UnsupportedEncodingException;  
  6. import java.net.HttpURLConnection;  
  7. import java.net.URL;  
  8.   
  9. public class AddressUtils {   
  10.    
  11.  public String getAddresses(String content, String encodingString)  
  12.    throws UnsupportedEncodingException {  
  13.   // 这里调用pconline的接口  
  14.   String urlStr = "http://ip.taobao.com/service/getIpInfo.php";  
  15.   // 从http://whois.pconline.com.cn取得IP所在的省市区信息  
  16.   String returnStr = this.getResult(urlStr, content, encodingString);  
  17.   if (returnStr != null) {  
  18.    // 处理返回的省市区信息  
  19.    System.out.println(returnStr);  
  20.    String[] temp = returnStr.split(",");  
  21.    if(temp.length<</span>3){  
  22.     return "0";//无效IP,局域网测试  
  23.    }  
  24.    String region = (temp[5].split(":"))[1].replaceAll(""", "");  
  25.    region = decodeUnicode(region);// 省份  
  26.     
  27.             String country = "";  
  28.             String area = "";  
  29.             // String region = "";  
  30.             String city = "";  
  31.             String county = "";  
  32.             String isp = "";  
  33.             for (int i = 0; i < temp.length; i++) {  
  34.                 switch (i) {  
  35.                 case 1:  
  36.                     country = (temp[i].split(":"))[2].replaceAll(""", "");  
  37.                     country = decodeUnicode(country);// 国家  
  38.                     break;  
  39.                     case 3:  
  40.                         area = (temp[i].split(":"))[1].replaceAll(""", "");  
  41.                         area = decodeUnicode(area);// 地区   
  42.                     break;  
  43.                     case 5:  
  44.                         region = (temp[i].split(":"))[1].replaceAll(""", "");  
  45.                         region = decodeUnicode(region);// 省份   
  46.                     break;   
  47.                     case 7:  
  48.                         city = (temp[i].split(":"))[1].replaceAll(""", "");  
  49.                         city = decodeUnicode(city);// 市区  
  50.                     break;   
  51.                     case 9:  
  52.                             county = (temp[i].split(":"))[1].replaceAll(""", "");  
  53.                             county = decodeUnicode(county);// 地区   
  54.                     break;  
  55.                     case 11:  
  56.                         isp = (temp[i].split(":"))[1].replaceAll(""", "");  
  57.                         isp = decodeUnicode(isp); // ISP公司  
  58.                     break;  
  59.                 }  
  60.             }  
  61.      
  62.     System.out.println(country+"="+area+"="+region+"="+city+"="+county+"="+isp);  
  63.    return region;  
  64.   }  
  65.   return null;  
  66.  }  
  67.    
  68.  private String getResult(String urlStr, String content, String encoding) {  
  69.   URL url = null;  
  70.   HttpURLConnection connection = null;  
  71.   try {  
  72.    url = new URL(urlStr);  
  73.    connection = (HttpURLConnection) url.openConnection();// 新建连接实例  
  74.    connection.setConnectTimeout(2000);// 设置连接超时时间,单位毫秒  
  75.    connection.setReadTimeout(2000);// 设置读取数据超时时间,单位毫秒  
  76.    connection.setDoOutput(true);// 是否打开输出流 true|false  
  77.    connection.setDoInput(true);// 是否打开输入流true|false  
  78.    connection.setRequestMethod("POST");// 提交方法POST|GET  
  79.    connection.setUseCaches(false);// 是否缓存true|false  
  80.    connection.connect();// 打开连接端口  
  81.    DataOutputStream out = new DataOutputStream(connection  
  82.      .getOutputStream());// 打开输出流往对端服务器写数据  
  83.    out.writeBytes(content);// 写数据,也就是提交你的表单 name=xxx&pwd=xxx  
  84.    out.flush();// 刷新  
  85.    out.close();// 关闭输出流  
  86.    BufferedReader reader = new BufferedReader(new InputStreamReader(  
  87.      connection.getInputStream(), encoding));// 往对端写完数据对端服务器返回数据  
  88.    // ,以BufferedReader流来读取  
  89.    StringBuffer buffer = new StringBuffer();  
  90.    String line = "";  
  91.    while ((line = reader.readLine()) != null) {  
  92.     buffer.append(line);  
  93.    }  
  94.    reader.close();  
  95.    return buffer.toString();  
  96.   } catch (IOException e) {  
  97.    e.printStackTrace();  
  98.   } finally {  
  99.    if (connection != null) {  
  100.     connection.disconnect();// 关闭连接  
  101.    }  
  102.   }  
  103.   return null;  
  104.  }  
  105.    
  106.  public static String decodeUnicode(String theString) {  
  107.   char aChar;  
  108.   int len = theString.length();  
  109.   StringBuffer outBuffer = new StringBuffer(len);  
  110.   for (int x = 0; x < len;) {  
  111.    aChar = theString.charAt(x++);  
  112.    if (aChar == '\\') {  
  113.     aChar = theString.charAt(x++);  
  114.     if (aChar == 'u') {  
  115.      int value = 0;  
  116.      for (int i = 0; i < 4; i++) {  
  117.       aChar = theString.charAt(x++);  
  118.       switch (aChar) {  
  119.       case '0':  
  120.       case '1':  
  121.       case '2':  
  122.       case '3':  
  123.       case '4':  
  124.       case '5':  
  125.       case '6':  
  126.       case '7':  
  127.       case '8':  
  128.       case '9':  
  129.        value = (value << 4) + aChar - '0';  
  130.        break;  
  131.       case 'a':  
  132.       case 'b':  
  133.       case 'c':  
  134.       case 'd':  
  135.       case 'e':  
  136.       case 'f':  
  137.        value = (value << 4) + 10 + aChar - 'a';  
  138.        break;  
  139.       case 'A':  
  140.       case 'B':  
  141.       case 'C':  
  142.       case 'D':  
  143.       case 'E':  
  144.       case 'F':  
  145.        value = (value << 4) + 10 + aChar - 'A';  
  146.        break;  
  147.       default:  
  148.        throw new IllegalArgumentException(  
  149.          "Malformed      encoding.");  
  150.       }  
  151.      }  
  152.      outBuffer.append((char) value);  
  153.     } else {  
  154.      if (aChar == 't') {  
  155.       aChar = '\t';  
  156.      } else if (aChar == 'r') {  
  157.       aChar = '\r';  
  158.      } else if (aChar == 'n') {  
  159.       aChar = '\n';  
  160.      } else if (aChar == 'f') {  
  161.       aChar = '\f';  
  162.      }  
  163.      outBuffer.append(aChar);  
  164.     }  
  165.    } else {  
  166.     outBuffer.append(aChar);  
  167.    }  
  168.   }  
  169.   return outBuffer.toString();  
  170.  }  
  171.  // 测试  
  172.  public static void main(String[] args) {  
  173.   AddressUtils addressUtils = new AddressUtils();  
  174.   // 测试ip 219.136.134.157 中国=华南=广东省=广州市=越秀区=电信  
  175.   String ip = "125.70.11.136";  
  176.   String address = "";  
  177.   try {  
  178.    address = addressUtils.getAddresses("ip="+ip, "utf-8");  
  179.   } catch (UnsupportedEncodingException e) {  
  180.    // TODO Auto-generated catch block  
  181.    e.printStackTrace();  
  182.   }  
  183.   System.out.println(address);  
  184.   // 输出结果为:广东省,广州市,越秀区  
  185.  }  
  186. }   

转载于:https://www.cnblogs.com/linewman/p/9918283.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值