互联网有很多接口可以实现通过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所在地:
http://ip.qq.com/cgi-bin/searchip?searchip1=ip

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

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

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

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

  1. java根据ip地址获取城市地域信息

    java根据ip地址获取城市地域信息 这里提供两个公开的接口,一个是阿里的,一个是新浪的 http://ip.taobao.com/service/getIpInfo.php?ip=123.139.9 ...

  2. java 根据IP地址获取地理位置

    来源:http://www.ipplus360.com/tech/api/ 来源:http://blog.csdn.net/Cryhelyxx/article/details/40862101 精确查 ...

  3. 6种根据IP地址获取相应城市信息的接口

    发这篇文章的最主要的目的就是给大家分享一个 免费快捷根据IP地址获取相应城市信息的接口--腾讯接口 在找到个腾讯接口之前,我试也用了很多接口,比如:淘宝,新浪等,下面挨个介绍 (ps:$ip是IP地址 ...

  4. Java 根据IP地址获取城市(ip2region)

    根据IP地址获取城市(ip2region) Ip2region是什么? Ip2region特性 99.9%准确率 标准化的数据格式 体积小 查询速度快 多查询客户端的支持 maven集成 小坑 Ip2 ...

  5. java通过ip地址获取相应对应的城市

    发现了一比较好的本地ip对应地址的库资源文件--做一下笔记. 官方网站 https://dev.maxmind.com/geoip/geoip2/geolite2/ 这里提供了免费的库文件,还有很好的 ...

  6. JAVA根据IP地址获取省份城市和经纬度(可获取国家名称 淘宝高德API如果是国外IP获取到的为空)

    所需jar包maven地址: <!-- 根据ip获取位置 --><dependency><groupId>com.maxmind.geoip2</groupI ...

  7. Java根据ip地址获取归属地

    由于最近比较忙,所以一直没有更新博客.今天有一点点时间,来分享一下项目中用到的一个需求功能点. 需求描述:获取用户真实ip和ip归属地 实现效果:  重点: IP地址工具类 import cn.hut ...

  8. 获取地区html代码,根据ip地址获取所在城市信息(省市县)代码

    [实例简介] [实例截图] [核心代码] 获取ip地址所在城市 var showIP = function(ip, remote_ip_info ){ if(remote_ip_info['ret'] ...

  9. 根据ip地址获取相关区域信息

    <?php /*** 基于淘宝的ipAPI 根据ip地址 返回所在地* @author L.D.B* 2012/09/12*/ header("Content-type:text/ht ...

最新文章

  1. Python学习笔记2 基本数据类型
  2. RabbitMQ中的虚拟主机、交换机、消息队列、绑定、消息
  3. ACM: 畅通工程-并查集-解题报告
  4. [BZOJ2654] tree
  5. ch6 列表和导航条
  6. C语言 课设 最新版 学生成绩管理系统
  7. 自定义sort函数第三个参数的规则
  8. 【ES9(2018)】String 扩展 标签模板里字符串转义
  9. /etc目录下重要文件解释
  10. [实战] 用数人云,部署弹性 ELK 集群就五步 1
  11. 我的HTML学习之路03
  12. tensorflow 模型权重导出
  13. UDT中的epoll
  14. 156.PHP多进程
  15. linux 下录音软件,linux下录音软件Audacity[zt]
  16. MFC windows程序设计(第三版)课后习题第二章
  17. python源码打包成exe、exe反编译、pyd加密防止反编译
  18. 模拟器:思科 创建Vlan,给2层交换机和3层交换机配置IP地址和子网掩码
  19. Python的鸭子类型
  20. Python一行代码实现正三角形

热门文章

  1. Winform窗口里的嵌入WPF的UserControl,关闭Winform父窗体的方法
  2. 华章IT图书书讯(2012年第9期)
  3. 7.1.3 TimePicker结合案例详解
  4. sqlserver中,如果正确得到中文的长度
  5. Android开发之自定义TabHost文字及背景(源代码分享)
  6. 阿里P7面试官告诉你:3-5年以上的Android开发如何深入进阶?Android中高级开发必须掌握哪些?
  7. 深入理解WMS(三):剖析Activity,View,Window之间的关系
  8. Handler消息机制(一):Linux的epoll机制
  9. Android Dialog 全屏、Dialog 底部显示
  10. Android SystemTrace使用攻略