获取IP的地理位置

最近写一个博客,获取IP的地理位置。查看了几个博客感觉比较复杂,下面是自己实现的一个:

package com.mo.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.Map;

import com.alibaba.fastjson.JSON;

public class IpAddress {

	/**
	 * 传人一个ip返回一个地理位置
	 * @param ip
	 * @return
	 */
	public static String changeIpToAddress(String ip) {
		Map<String, String> map = changeJsonToMap(getAddressByIp(ip));
		String country = revert(map.get("country"));
		String province = revert(map.get("province"));
		String city = revert(map.get("city"));
		return country + " " + province + "省 " + city + "市";
	}
	
	/**
	 * 传人一个ip返回一个地理位置(只有省市)
	 * @param ip
	 * @return
	 */
	public static String changeIpToAddressOnlyProAndCity(String ip) {
		Map<String, String> map = changeJsonToMap(getAddressByIp(ip));
		String province = revert(map.get("province"));
		String city = revert(map.get("city"));
		return province + " " + city;
	}
	
	/**
	 * 传人ip地址,出来的是unicode编码的json数据
	 * @return json字符串
	 * @throws IOException
	 */
	private static String getAddressByIp(String ip) {
		StringBuffer result;
		try {
			URL url = new URL("http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=json&ip=" + ip);
			URLConnection conn = url.openConnection();
			BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream(),"UTF-8"));
			String line = null;
			result = new StringBuffer();
			while((line = reader.readLine()) != null) {
				result.append(line);
			}	
			reader.close();
			return result.toString();
		} catch (IOException e) {
			System.out.println("ip没有找到");
		}
		return "无法找到ip地址";
	}


	/**
	 * 将传人的JSON字符串封装为Map
	 * @param json字符串
	 * @return 封装了map的json的数据
	 */
	private static Map<String, String> changeJsonToMap(String address) {
		Map maps = (Map)JSON.parse(address);  
		return maps;
	}

	/**
	 * 将unicode转换为文字
	 * @param 传人一个unicode编码
	 * @return 返回一个汉字
	 */
	private static String revert(String str) 
	{ 
		str = (str == null ? "" : str); 
		if (str.indexOf("\\u") == -1)//如果不是unicode码则原样返回 
			return str; 

		StringBuffer sb = new StringBuffer(1000); 

		for (int i = 0; i < str.length() - 6;) 
		{ 
			String strTemp = str.substring(i, i + 6); 
			String value = strTemp.substring(2); 
			int c = 0; 
			for (int j = 0; j < value.length(); j++) 
			{ 
				char tempChar = value.charAt(j); 
				int t = 0; 
				switch (tempChar) 
				{ 
				case 'a': 
					t = 10; 
					break; 
				case 'b': 
					t = 11; 
					break; 
				case 'c': 
					t = 12; 
					break; 
				case 'd': 
					t = 13; 
					break; 
				case 'e': 
					t = 14; 
					break; 
				case 'f': 
					t = 15; 
					break; 
				default: 
					t = tempChar - 48; 
					break; 
				} 

				c += t * ((int) Math.pow(16, (value.length() - j - 1))); 
			} 
			sb.append((char) c); 
			i = i + 6; 
		} 
		return sb.toString(); 
	}
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值