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

文章来源:http://hejianke83.blog.163.com/blog/static/60765162012101694919149/

猜你喜欢

转载自blog.csdn.net/i_huli/article/details/80067189