用的是http://ip.taobao.com/service/getIpInfo.php接口。
这个也是网上找的,但是我已经修改测试过了,是可用的,在这里先感谢原作者。

package com.rookie.mapper;import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;/*** 根据IP地址获取详细的地域信息*/
public class Test {/*** * @param content  请求的参数 格式为:name=xxx&pwd=xxx** @param encodingString 服务器端请求编码。如GBK,UTF-8等* @return* @throws UnsupportedEncodingException*/public String getAddresses(String content, String encodingString) throws UnsupportedEncodingException {// 这里调用pconline的接口String urlStr = "http://ip.taobao.com/service/getIpInfo.php";// 从http://whois.pconline.com.cn取得IP所在的省市区信息String returnStr = this.getResult(urlStr, content, encodingString);if (returnStr != null) {// 处理返回的省市区信息System.out.println(returnStr);String[] temp = returnStr.split(",");if (temp.length < 3) {return "0";//无效IP,局域网测试}
//            String region = (temp[5].split(":"))[1].replaceAll("\"", "");
//            region = decodeUnicode(region);// 省份String country = "";String area = "";String region = "";String city = "";String county = "";String isp = "";for (int i = 0; i < temp.length; i++) {switch (i) {case 1:country =(temp[i].split(":"))[2].replaceAll("\"", "");country = decodeUnicode(country);//国家break;case 3:area = (temp[i].split(":"))[1].replaceAll("\"", "");area = decodeUnicode(area);//地区break;case 5:region = (temp[i].split(":"))[1].replaceAll("\"", "");region = decodeUnicode(region);//省份break;case 7:city = (temp[i].split(":"))[1].replaceAll("\"", "");city = decodeUnicode(city);//市区break;case 9:county = (temp[i].split(":"))[1].replaceAll("\"", "");county = decodeUnicode(county);//地区break;case 11:isp = (temp[i].split(":"))[1].replaceAll("\"", "");isp = decodeUnicode(isp);//ISP公司break;}}System.out.println(country + "=" + area + "=" + region + "=" + city + "=" + county + "=" + isp);return region;}return null;}/*** @param urlStr   请求的地址* @param content  请求的参数 格式为:name=xxx&pwd=xxx* @param encoding 服务器端请求编码。如GBK,UTF-8等* @return*/private String getResult(String urlStr, String content, String encoding) {URL url = null;HttpURLConnection connection = null;try {url = new URL(urlStr);connection = (HttpURLConnection) url.openConnection();// 新建连接实例connection.setConnectTimeout(2000);// 设置连接超时时间,单位毫秒connection.setReadTimeout(2000);// 设置读取数据超时时间,单位毫秒connection.setDoOutput(true);// 是否打开输出流 true|falseconnection.setDoInput(true);// 是否打开输入流true|falseconnection.setRequestMethod("POST");// 提交方法POST|GETconnection.setUseCaches(false);// 是否缓存true|falseconnection.connect();// 打开连接端口DataOutputStream out = new DataOutputStream(connection.getOutputStream());// 打开输出流往对端服务器写数据out.writeBytes(content);// 写数据,也就是提交你的表单 name=xxx&pwd=xxxout.flush();// 刷新out.close();// 关闭输出流BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), encoding));// 往对端写完数据对端服务器返回数据// ,以BufferedReader流来读取StringBuffer buffer = new StringBuffer();String line = "";while ((line = reader.readLine()) != null) {buffer.append(line);}reader.close();return buffer.toString();} catch (IOException e) {e.printStackTrace();} finally {if (connection != null) {connection.disconnect();// 关闭连接}}return null;}/*** unicode 转换成 中文** @param theString* @return**/public static String decodeUnicode(String theString) {char aChar;int len = theString.length();StringBuffer outBuffer = new StringBuffer(len);for (int x = 0; x < len; ) {aChar = theString.charAt(x++);if (aChar == '\\') {aChar = theString.charAt(x++);if (aChar == 'u') {int value = 0;for (int i = 0; i < 4; i++) {aChar = theString.charAt(x++);switch (aChar) {case '0':case '1':case '2':case '3':case '4':case '5':case '6':case '7':case '8':case '9':value = (value << 4) + aChar - '0';break;case 'a':case 'b':case 'c':case 'd':case 'e':case 'f':value = (value << 4) + 10 + aChar - 'a';break;case 'A':case 'B':case 'C':case 'D':case 'E':case 'F':value = (value << 4) + 10 + aChar - 'A';break;default:throw new IllegalArgumentException("Malformed      encoding.");}}outBuffer.append((char) value);} else {if (aChar == 't') {aChar = '\t';} else if (aChar == 'r') {aChar = '\r';} else if (aChar == 'n') {aChar = '\n';} else if (aChar == 'f') {aChar = '\f';}outBuffer.append(aChar);}} else {outBuffer.append(aChar);}}return outBuffer.toString();}// 测试public static void main(String[] args) {Test addressUtils = new Test();// 测试ip 219.136.134.157 中国=华南=广东省=广州市=越秀区=电信    122.49.20.247--北京String ip = "219.136.134.157";String address = "";try {address = addressUtils.getAddresses("ip=" + ip, "utf-8");} catch (UnsupportedEncodingException e) {// TODO Auto-generated catch blocke.printStackTrace();}System.out.println(address);   // 输出结果为:广东省}
}

今天看了之后发现上面的代码似乎有些多余,对于一些情况不用写的那么复杂,下面这个相当于简化版(即没有编码处理),仅做参考:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Map;/*** Created by czy on 2018/11/23.* 地址相关*/
public class AddressUtil {/*** @param ipAddr ip地址* @return 位置信息*/public static Map<String, Object> getAddressByIP(String ipAddr) {String requestURL = "http://ip.taobao.com/service/getIpInfo.php?ip=" + ipAddr;//从 http://whois.pconline.com.cn 取得IP所在的省市区信息URL url = null;HttpURLConnection connection = null;BufferedReader reader = null;try {url = new URL(requestURL);connection = (HttpURLConnection) url.openConnection();connection.setConnectTimeout(2000);connection.setReadTimeout(2000);connection.connect();reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));String lines;StringBuilder sb = new StringBuilder();while ((lines = reader.readLine()) != null) {sb.append(lines);}reader.close();Map<String, Object> result = FormatUtil.parseJSON2Map(sb.toString());return result;} catch (MalformedURLException ex) {ex.printStackTrace();return null;} catch (IOException ex) {ex.printStackTrace();return null;} finally {connection.disconnect();}}public static void main(String[] args) {Map<String, Object> addressByIP = getAddressByIP("43.224.80.0");if (addressByIP == null) {System.out.println("根据IP获取地址出错");return;}System.out.println(addressByIP.toString());Map<String, Object> data = (Map<String, Object>) addressByIP.get("data");System.out.println(data.toString());String area = String.valueOf(data.get("area"));//区域-华南华北等String area_id = String.valueOf(data.get("area_id"));String city = String.valueOf(data.get("city"));//城市String city_id = String.valueOf(data.get("city_id"));String country = String.valueOf(data.get("country"));//国家String country_id = String.valueOf(data.get("country_id"));String county = String.valueOf(data.get("county"));//县String county_id = String.valueOf(data.get("county_id"));String isp = String.valueOf(data.get("isp"));//运营商String isp_id = String.valueOf(data.get("isp_id"));String region = String.valueOf(data.get("region"));//省份String region_id = String.valueOf(data.get("region_id"));System.out.println(area + "--" + country + "--" + region + "--" + city + "--" + county + "--" + isp);}
}

下面是简化版对应的返回结果:

{code=0, data={"area":"","area_id":"","city":"合肥","city_id":"340100","country":"中国","country_id":"CN","county":"XX","county_id":"xx","ip":"43.224.80.0","isp":"联通","isp_id":"100026","region":"安徽","region_id":"340000"}}
{"area":"","area_id":"","city":"合肥","city_id":"340100","country":"中国","country_id":"CN","county":"XX","county_id":"xx","ip":"43.224.80.0","isp":"联通","isp_id":"100026","region":"安徽","region_id":"340000"}
--中国--安徽--合肥--XX--联通

根据ip地址查询地址信息相关推荐

  1. 根据ip地址查询城市信息

    需要先购买商品 https://apis.baidu.com/store/detail/31e507c6-caa1-4b25-8786-3af1543a79b9?track=qfcip&pag ...

  2. H3C交换机运维之-通过已知终端MAC地址查询接入交换机接口

    注意: 此步骤只适用于终端和接入交换机互通不知道是哪个口的情况(请先确保终端与接入交换机线路正常) 需要满足条件 1.知道管理vlan(这里模拟为vlan900) 2.知道新入网终端MAC地址后4位( ...

  3. html显示用户ipv6地址,IPv6地址查询

    根据IPv6地址查询地址位置.关于IPv6点击这里了解 IPv6,编码规则如下: (1)IPv6地址为128位长,通常写作8组,每组四个字符(换算为16位长),组与组之间用半角":" ...

  4. IP地址查询接口,根据IP地址查询城市地区等信息

    1.建议使用 http://ip-api.com/json/?lang=zh-CN 2.IP地址查询接口:http://apis.juhe.cn/ip/ip2addr 要先去https://www.j ...

  5. 怎么查询局域网内全部电脑IP和mac地址等信息?

    在局域网内查询在线主机的IP一般比较简单,但局域网内全部电脑的IP怎么才能够查到呢?查询到IP后我还要知道对方的一些详细信息(如MAC地址.电脑名称等)该怎么查询呢???或者用命令 :arp -a   ...

  6. php 本地mysql 代码_基于本地数据库的 IP 地址查询 PHP 源码

    * 纯真 IP 数据库查询 * * 参考资料: * - 纯真 IP 数据库 http://www.cz88.net/ip/ * - PHP 读取纯真IP地址数据库 http://ju.outofmem ...

  7. 怎么查询局域网内全部电脑IP和mac地址..

    在局域网内查询在线主机的IP一般比较简单,但局域网内全部电脑的IP怎么才能够查到呢?查询到IP后我还要知道对方的一些详细信息(如MAC地址.电脑名称等)该怎么查询呢??? 工具/原料 Windows ...

  8. win7个人计算机的ip地址,win7计算机ip地址查询_win7本机ip地址查询

    2016-12-09 11:40:21 查找计算机的ip地址的方法:点击你的电脑桌面左下角的"开始"找到"运行"点击运行, 在出现的对话框里面输入"c ...

  9. java webservice ip_通过Web Service实现IP地址查询功能的示例

    实例01 实现一个简单的Web服务访问 本实例将实现IP地址查询接口服务,根据用户传入的IP地址返回IP所在的省.市.地区,实例中将会用到IP地址库用于查询信息,由于数据较多,所以读者可在光盘资源文件 ...

最新文章

  1. 一文搞懂 CountDownLatch 用法和源码!
  2. 云南省2021高考成绩查询时间,2021云南高考成绩什么时候几点可以查
  3. 上班后咋防控?分享一份指南
  4. GNU make manual 翻译(四十三)
  5. 模板:二维凸包(计算几何)
  6. html tab与jQuery,使用jquery实现div的tab切换实例代码
  7. linux mysql 临时文件_linux下mysql自动备份数据库与自动删除临时文件
  8. Mac安装mysql数据库【亲测有用】
  9. python gil锁_python--GIL锁
  10. Spark SQL join的三种实现方式
  11. 【NIO】dawn在buffer用法
  12. Python 装饰器总结
  13. 收藏十二:ExtJs
  14. 字节码指令之操作数栈管理指令
  15. 转载——巨详细的MD5加盐,大佬详解
  16. python爬取酷狗音乐top500_爬取酷狗音乐Top500
  17. 计算机专业论文周进展300字,论文进展情况记录300字_论文周进展情况记录文库_论文进展情况18篇记录...
  18. 编译MKL50.1 (for 一加手机)
  19. 荣耀20搭载鸿蒙,荣耀20新机发布 搭载鸿蒙系统荣耀20详细参数
  20. 后级功放机与单声道功放机的功能有哪些区别?

热门文章

  1. Android插件化探索与发现
  2. 三角形3边算面积程序 C语言
  3. 微软“创新杯”全球学生大赛
  4. SAP那些事-职业篇-8-顾问“三宝”
  5. k3梅林单线双拨教程_华硕AC86U单线双拨设置方法。。。
  6. 令狐冲和TCP/IP协议的第三层协议的关系
  7. IM,小视频, 直播 几大云平台对比选择
  8. 我的世界服务器无限挑战,我的世界:30名玩家,被关在mc服务器整整一个月!一场人性的实验...
  9. 我的高三 (2)[百日誓师后.]
  10. sublime中检查php语法错误