Java常见帮助类(7)IP查询地址帮助类

/**
 * IP工具类
 */
public class IpUtil
{
    
    private static Logger logger = Logger.getLogger(IpUtil.class);
    
    /**
     * 获取登录用户的IP地址
     *
     * @param request
     * @return
     */
    public static String getIpAddr(HttpServletRequest request)
    {
        String ip = request.getHeader("x-forwarded-for");
        if ( ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip) )
        {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if ( ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip) )
        {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if ( ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip) )
        {
            ip = request.getRemoteAddr();
        }
        if ( ip.equals("0:0:0:0:0:0:0:1") )
        {
            ip = "本地";
        }
        if ( ip.split(",").length > 1 )
        {
            ip = ip.split(",")[0];
        }
        
        return ip;
    }
    
    /**
     * 通过IP获取地址
     *
     * @param ip
     * @return
     */
    public static String getIpInfo(String ip)
    {
        String info = "";
        try
        {
            URL url = new URL("http://ip.taobao.com/service/getIpInfo.php?ip=" + ip);
            HttpURLConnection htpcon = (HttpURLConnection) url.openConnection();
            htpcon.setRequestMethod("GET");
            htpcon.setDoOutput(true);
            htpcon.setDoInput(true);
            htpcon.setUseCaches(false);
            
            InputStream in = htpcon.getInputStream();
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(in));
            StringBuffer temp = new StringBuffer();
            String line = bufferedReader.readLine();
            while (line != null)
            {
                temp.append(line).append("\r\n");
                line = bufferedReader.readLine();
            }
            bufferedReader.close();
            
            return temp.toString();
            
        }
        catch (MalformedURLException e)
        {
            logger.error(e);
        }
        catch (ProtocolException e)
        {
            logger.error(e);
        }
        catch (IOException e)
        {
            logger.error(e);
        }
        return info;
    }
    
    /**
     * 通过IP获取国家
     *
     * @param ip
     * @return
     */
    public static String getCountryByIp(String ip)
    {
        
        JSONObject obj = (JSONObject) JSON.parse(getIpInfo(ip));
        String country = "";
        if ( obj != null && obj.getIntValue("code") == 0 )
        {
            JSONObject data = obj.getJSONObject("data");
            country = data.getString("country") + " ";
        }
        return country.trim();
        
    }
    
    /**
     * 通过IP获取省份
     *
     * @param ip
     * @return
     */
    public static String getRegionByIp(String ip)
    {
        
        JSONObject obj = (JSONObject) JSON.parse(getIpInfo(ip));
        String region = "";
        if ( obj != null && obj.getIntValue("code") == 0 )
        {
            JSONObject data = obj.getJSONObject("data");
            region = data.getString("region") + " ";
        }
        return region.trim();
        
    }
    
    /**
     * 通过IP获取城市
     *
     * @param ip
     * @return
     */
    public static String getCityByIp(String ip)
    {
        
        JSONObject obj = (JSONObject) JSON.parse(getIpInfo(ip));
        String city = "";
        if ( obj != null && obj.getIntValue("code") == 0 )
        {
            JSONObject data = obj.getJSONObject("data");
            city = data.getString("city") + " ";
        }
        return city.trim();
        
    }
    
    /**
     * 通过IP获取国家 地市 行业
     *
     * @param ip
     * @return
     */
    public static String getAddressByIp(String ip)
    {
        
        JSONObject obj = (JSONObject) JSON.parse(getIpInfo(ip));
        StringBuffer address = new StringBuffer();
        if ( obj != null && obj.getIntValue("code") == 0 )
        {
            JSONObject data = obj.getJSONObject("data");
            // 国家
            String country = data.getString("country");
            // 省
            String region = data.getString("region");
            // 地市
            String city = data.getString("city");
            
            if ( StringUtils.isNoneBlank(country) )
            {
                address.append(country);
                address.append(" ");
            }
            if ( StringUtils.isNoneBlank(region) )
            {
                address.append(region);
                address.append(" ");
            }
            if ( StringUtils.isNoneBlank(city) )
            {
                address.append(city);
                address.append(" ");
            }
        }
        return address.toString().trim();
        
    }
    
    /**
     * @描述: 根据域名获取ip
     */
    public static String getIPByDomain(String name)
    {
        InetAddress address = null;
        try
        {
            address = InetAddress.getByName(name);
        }
        catch (UnknownHostException e)
        {
            logger.error(e);
        }
        return address.getHostAddress().trim().toString();
    }
    
    public static void main(String[] args)
    {
        System.out.println(getIpInfo("117.158.3.54"));
    }
    
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值