JAVA通过IP获取地理位置(地区)

转载:http://blog.csdn.net/Cryhelyxx/article/details/40862101
1. 第三方API
ps:下面参数ip:	218.192.3.42 用于测试
	淘宝API:http://ip.taobao.com/service/getIpInfo.php?ip=218.192.3.42
	新浪API:http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=json&ip=218.192.3.42
	pconline API:http://whois.pconline.com.cn/
	百度API:http://api.map.baidu.com/location/ip?ip=218.192.3.42
2. 工具类
AddressUtils.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. /** 
  10.  * 根据IP地址获取详细的地域信息 
  11.  * 淘宝API : http://ip.taobao.com/service/getIpInfo.php?ip=218.192.3.42 
  12.  * 新浪API : http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=json&ip=218.192.3.42 
  13.  * @File AddressUtils.java 
  14.  * @Package org.gditc.weicommunity.util 
  15.  * @Description TODO 
  16.  * @Copyright Copyright © 2014 
  17.  * @Site https://github.com/Cryhelyxx 
  18.  * @Blog http://blog.csdn.net/Cryhelyxx 
  19.  * @Email cryhelyxx@gmail.com 
  20.  * @Company GDITC 
  21.  * @Date 2014年11月6日 下午1:46:37 
  22.  * @author Cryhelyxx 
  23.  * @version 1.0 
  24.  */  
  25. public class AddressUtils {  
  26.     /** 
  27.      *  
  28.      * @param content 
  29.      *            请求的参数 格式为:name=xxx&pwd=xxx 
  30.      * @param encoding 
  31.      *            服务器端请求编码。如GBK,UTF-8等 
  32.      * @return 
  33.      * @throws UnsupportedEncodingException 
  34.      */  
  35.     public static String getAddresses(String content, String encodingString)  
  36.             throws UnsupportedEncodingException {  
  37.         // 这里调用淘宝API  
  38.         String urlStr = "http://ip.taobao.com/service/getIpInfo.php";  
  39.         // 从http://whois.pconline.com.cn取得IP所在的省市区信息  
  40.         String returnStr = getResult(urlStr, content, encodingString);  
  41.         if (returnStr != null) {  
  42.             // 处理返回的省市区信息  
  43.             System.out.println("(1) unicode转换成中文前的returnStr : " + returnStr);  
  44.             returnStr = decodeUnicode(returnStr);  
  45.             System.out.println("(2) unicode转换成中文后的returnStr : " + returnStr);  
  46.             String[] temp = returnStr.split(",");  
  47.             if(temp.length<3){  
  48.                 return "0";//无效IP,局域网测试  
  49.             }  
  50.             return returnStr;  
  51.         }  
  52.         return null;  
  53.     }  
  54.     /** 
  55.      * @param urlStr 
  56.      *            请求的地址 
  57.      * @param content 
  58.      *            请求的参数 格式为:name=xxx&pwd=xxx 
  59.      * @param encoding 
  60.      *            服务器端请求编码。如GBK,UTF-8等 
  61.      * @return 
  62.      */  
  63.     private static 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.     /** 
  101.      * unicode 转换成 中文 
  102.      *  
  103.      * @author fanhui 2007-3-15 
  104.      * @param theString 
  105.      * @return 
  106.      */  
  107.     public static String decodeUnicode(String theString) {  
  108.         char aChar;  
  109.         int len = theString.length();  
  110.         StringBuffer outBuffer = new StringBuffer(len);  
  111.         for (int x = 0; x < len;) {  
  112.             aChar = theString.charAt(x++);  
  113.             if (aChar == '\\') {  
  114.                 aChar = theString.charAt(x++);  
  115.                 if (aChar == 'u') {  
  116.                     int value = 0;  
  117.                     for (int i = 0; i < 4; i++) {  
  118.                         aChar = theString.charAt(x++);  
  119.                         switch (aChar) {  
  120.                         case '0':  
  121.                         case '1':  
  122.                         case '2':  
  123.                         case '3':  
  124.                         case '4':  
  125.                         case '5':  
  126.                         case '6':  
  127.                         case '7':  
  128.                         case '8':  
  129.                         case '9':  
  130.                             value = (value << 4) + aChar - '0';  
  131.                             break;  
  132.                         case 'a':  
  133.                         case 'b':  
  134.                         case 'c':  
  135.                         case 'd':  
  136.                         case 'e':  
  137.                         case 'f':  
  138.                             value = (value << 4) + 10 + aChar - 'a';  
  139.                             break;  
  140.                         case 'A':  
  141.                         case 'B':  
  142.                         case 'C':  
  143.                         case 'D':  
  144.                         case 'E':  
  145.                         case 'F':  
  146.                             value = (value << 4) + 10 + aChar - 'A';  
  147.                             break;  
  148.                         default:  
  149.                             throw new IllegalArgumentException(  
  150.                                     "Malformed      encoding.");  
  151.                         }  
  152.                     }  
  153.                     outBuffer.append((char) value);  
  154.                 } else {  
  155.                     if (aChar == 't') {  
  156.                         aChar = '\t';  
  157.                     } else if (aChar == 'r') {  
  158.                         aChar = '\r';  
  159.                     } else if (aChar == 'n') {  
  160.                         aChar = '\n';  
  161.                     } else if (aChar == 'f') {  
  162.                         aChar = '\f';  
  163.                     }  
  164.                     outBuffer.append(aChar);  
  165.                 }  
  166.             } else {  
  167.                 outBuffer.append(aChar);  
  168.             }  
  169.         }  
  170.         return outBuffer.toString();  
  171.     }  
  172.     // 测试  
  173.     public static void main(String[] args) {  
  174.         AddressUtils addressUtils = new AddressUtils();  
  175.         // 测试ip 219.136.134.157 中国=华南=广东省=广州市=越秀区=电信  
  176.         String ip = "122.49.20.247";  
  177.         String address = "";  
  178.         try {  
  179.             address = addressUtils.getAddresses("ip="+ip, "utf-8");  
  180.         } catch (UnsupportedEncodingException e) {  
  181.             // TODO Auto-generated catch block  
  182.             e.printStackTrace();  
  183.         }  
  184.         System.out.println(address);  
  185.         // 输出结果为:广东省,广州市,越秀区  
  186.     }  
  187. }  


  • 3
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值