AddressUtils.java 1.16 KB
package com.huaheng.common.utils;

import com.alibaba.fastjson.JSONObject;
import com.huaheng.common.utils.http.HttpUtils;
import com.huaheng.framework.config.HuaHengConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 获取地址类
 * 
 * @author huaheng
 */
public class AddressUtils
{
    private static final Logger log = LoggerFactory.getLogger(AddressUtils.class);

    public static final String IP_URL = "http://ip.taobao.com/service/getIpInfo.php";

    public static String getRealAddressByIp(String ip)
    {
        String address = "";
        try
        {
            if (HuaHengConfig.isAddressEnabled())
            {
                address = HttpUtils.sendPost(IP_URL, "ip=" + ip);
                JSONObject json = JSONObject.parseObject(address);
                JSONObject object = json.getObject("data", JSONObject.class);
                String region = object.getString("region");
                String city = object.getString("city");
                address = region + " " + city;
            }
        }
        catch (Exception e)
        {
            log.error("获取地理位置异常:", e);
        }
        return address;
    }
}