import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;/***  IP 地址获取* @author ITchen* @since 2016/6/29 17:56*/
public class IpAddressUtil {/****/public static String getAddresses(String ip)throws IOException {// 调用淘宝地理位置接口String urlStr = "http://ip.taobao.com/service/getIpInfo.php?ip="+ip;// 从http://whois.pconline.com.cn取得IP所在的省市区信息String returnStr = IpAddressUtil.getResult(urlStr,"utf-8");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 encoding 编码* @throws IOException*/private static String getResult(String urlStr, String encoding) throws IOException {URL url = null;HttpURLConnection connection = null;try {url = new URL(urlStr);connection = (HttpURLConnection) url.openConnection();// 新建连接实例connection.setConnectTimeout(5000);// 设置连接超时时间,单位毫秒connection.setReadTimeout(5000);// 设置读取数据超时时间,单位毫秒connection.setDoInput(true);// 是否打开输入流true|falseconnection.setUseCaches(false);// 是否缓存true|falseconnection.connect();// 请求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 转换字符串*/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) {String ip = "125.70.11.136";String address = "";try {address = IpAddressUtil.getAddresses(ip);} catch (UnsupportedEncodingException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {e.printStackTrace();}System.out.println(address);}
}

通过ip获取地理位置相关推荐

  1. php 获取 ip 并根据 ip 获取地理位置

    php 获取 ip 有很多方法,这里选了其中一种 if(getenv('HTTP_CLIENT_IP')){$onlineip = getenv('HTTP_CLIENT_IP'); } elseif ...

  2. 根据IP获取地理位置信息 — Golang

    根据IP获取地理位置信息 - Golang 1 介绍 1.1 ip2region 1.2 geoip2-golang 1.3 总结 2 使用 2.1 ip2region 2.2 geoip2-gola ...

  3. php查询ip归属地api接口_php调用新浪接口查询ip获取地理位置(ip归属地查询)

    php调用新浪接口查询ip获取地理位置(ip归属地查询) 发布于 2014-11-17 08:35:58 | 115 次阅读 | 评论: 0 | 来源: 网友投递 PHP开源脚本语言PHP(外文名: ...

  4. 分享几个IP获取地理位置的API

    淘宝(适用国内外城市) 一.请求接口(GET): https://ip.taobao.com/outGetIpInfo?ip=IP地址&accessKey=alibaba-inc 二.返回数据 ...

  5. php通过ip地址查询位置,PHP通过IP获取地理位置

    //地址方法 function get_client_dizhi($ip){ $doc = new DOMDocument(); $doc->load('http://www.youdao.co ...

  6. 分享几个IP获取地理位置的API接口

    文章目录 360获取本机IP.地区及运营商 360获取指定IP的地区及运营商 ip508获取指定IP.地区及所处位置 淘宝获取本机IP地址 淘宝获取IP详细信息 搜狐IP地址查询接口 新浪IP地址查询 ...

  7. Android根据IP获取地理位置,精确到经纬度

    摘要:每一个联网的设备都有一个IP地址,移动设备也不例外,如何定位Android手机当前的地理位置呢?很简单,我们依然可以利用 IP地址来获取Android手机的当前位置,可以精确到经纬度. 本文就直 ...

  8. 通过GeoIP2分析访问者IP获取地理位置信息

    MaxMind GeoIP2 服务能识别互联网用户的地点位置与其他特征,应用广泛,包括个性化定制内容.诈欺检测.广告定向.网站流量分析.执行规定.地理目标定位.地理围栏定位 (geo-fencing) ...

  9. python 通过ip获取城市_python 根据ip获取地理位置

    !/usr/bin/python coding=utf-8 import dpkt import socket import pygeoip import optparse gi = pygeoip. ...

最新文章

  1. 直接依赖,间接依赖,可选依赖,排除依赖,依赖冲突
  2. 【高级数据结构】[SPOJ QTREE]树链剖分/动态树各一模板
  3. DialogFragment 全屏显示
  4. Fishe向量Fisher Vecotr(二)
  5. 串口通信模块4:串口操作自定义类(1)
  6. 关于计算机组成的ppt,计算机组成课件.ppt
  7. JAVA单例模式:懒汉式,饿汉式
  8. ThreadLocal应用与原理分析
  9. 拿访问网站用户IP 纯JS实现
  10. 如何 方法内指令重排 进制_谈谈指令重排
  11. Linux 关闭网络管理服务
  12. 2018年黑龙江由俄进口原油2725.2万吨同比增加67.1%
  13. uni-app 图片适配 动态计算图片高度
  14. java 创建进程_Linux下创建进程简介
  15. 2019年第五届计蒜之道复赛总结
  16. 机器视觉入门 Visual Studio 2015 配置 Opencv3.2
  17. C语言正则表达式详解 regcomp() regexec() regfree()详解
  18. cesium根据两点获取航向角(heading)与俯仰角(pitch)
  19. ValueError: Only images with 2 spatial dimensions are supported. If using with color/multichannel im
  20. 关于头歌C/C++编程实训数组实训朋友圈点赞的一个题解

热门文章

  1. 图表生成pdf,出坑经历
  2. 臻鑫永晖:1万存活期1年利息多少?附核算公式
  3. 一个HR人给应届生的面试建议【推荐】
  4. 《海边的卡夫卡》阅读琐记
  5. 3dsMax 修改为中文
  6. TpyeScript基础
  7. 微信公众号/订阅号开通留言功能
  8. c语言写plc程序正反转,西门子PLC控制电机正反转编程实例!
  9. 第三回 利器,我的DHCP (转)
  10. 【亲测】独家更新CcPay多商户码支付系统,码支付易支付+个人支付宝微信二维码收款app监控+搭建教程