zoukankan      html  css  js  c++  java
  • 【转载】 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":"u4e2du56fd","area":"u534eu5317",
    "region":"u5317u4eacu5e02","city":"u5317u4eacu5e02","county":"","isp":"u7535u4fe1",
    "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":"u4e2du56fd","province":"u5e7fu4e1c","city":"u5e7fu5dde","district":"","isp":"u6559u80b2u7f51","type":"u5b66u6821","desc":"u5e7fu5ddeu5927u5b66u7ebau7ec7u670du88c5u5b66u9662"};
    通过jqry 获取相应的数据
    $.getScript('数据接口',function(){
    //新浪:remote_ip_info.country
    }) 

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

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

    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 0temp.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 0len;)  
    111.    aChar theString.charAt(x++);  
    112.    if (aChar == '\' 
    113.     aChar theString.charAt(x++);  
    114.     if (aChar == 'u' 
    115.      int value 0 
    116.      for (int 04i++)  
    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 << 4aChar '0' 
    130.        break 
    131.       case 'a' 
    132.       case 'b' 
    133.       case 'c' 
    134.       case 'd' 
    135.       case 'e' 
    136.       case 'f' 
    137.        value (value << 410 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 << 410 aChar 'A' 
    146.        break 
    147.       default 
    148.        throw new IllegalArgumentException 
    149.          "Malformed      encoding.");  
    150.        
    151.       
    152.      outBuffer.append((charvalue);  
    153.     else  
    154.      if (aChar == 't' 
    155.       aChar ' ' 
    156.      else if (aChar == 'r' 
    157.       aChar ' ' 
    158.      else if (aChar == 'n' 
    159.       aChar ' ' 
    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.   
  • 相关阅读:
    提交订单的时候,实现拦截,登录成功后跳转回之前的页面;使用redis生成指定起始位置的id
    iOS Outlets Referencing Outlets
    iOS 常用控件 参数
    iOS UIImageView设置为圆形
    iOS Xcode个人常用插件
    iOS Xcode注释的几种使用方法
    MFC MSBDutyTable下载地址
    MFC HTTP访问URL
    MFC 程序以管理员权限运行
    MFC 打开文件夹选择框并获取文件夹路径
  • 原文地址:https://www.cnblogs.com/linewman/p/9918283.html
Copyright © 2011-2022 走看看