IP 获取和 IP定位

IP 获取和 IP定位


1.主类


import javax.servlet.http.HttpServletRequest;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.httpclient.NameValuePair;

import java.util.HashMap;

/**
 * Created by 谭健 on 2017/8/11. 14:53.
 * © All Rights Reserved.
 * 

* 网络相关工具类 */ public class NetWork { /** * 获取一个请求的发起IP * * @param request HttpServletRequest * @return String类型的ip */ public static String getRemoteIp(HttpServletRequest request) { String ip; ip = request.getHeader("x-forwarded-for"); if (isNullIp(ip)) { ip = request.getHeader("Proxy-Client-IP"); } if (isNullIp(ip)) { ip = request.getHeader("WL-Proxy-Client-IP"); } if (isNullIp(ip)) { ip = request.getHeader("HTTP_CLIENT_IP"); } if (isNullIp(ip)) { ip = request.getHeader("HTTP_X_FORWARDED_FOR"); } if (isNullIp(ip)) { ip = request.getRemoteAddr(); } if (ip.contains(",")) { ip = ip.split(",")[0]; } if ("0.0.0.0.0.0.0.1".equals(ip) || "0.0.0.0.0.0.0.1%0".equals(ip)) { ip = "127.0.0.1"; } return ip; } /** * IP空验证 * * @param ip * @return */ private static boolean isNullIp(final String ip) { return ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip); } public static TaobaoIPLibResult getRemoteIpRegionalInfo(HttpServletRequest request) { String remoteIp = getRemoteIp(request); return getIpRegionalInfo(remoteIp); } public static TaobaoIPLibResult getIpRegionalInfo(String ip) { boolean isSuccess; IPAddressInfo addressInfo; JSONObject data; HashMap resultMap; String postResult; TaobaoIPLibResult result = new TaobaoIPLibResult(); NameValuePair[] nameValuePairs = { new NameValuePair("ip", ip) }; postResult = Post.run("http://ip.taobao.com/service/getIpInfo.php", nameValuePairs); resultMap = JSON.parseObject(postResult, HashMap.class); isSuccess = (Integer) resultMap.get("code") == 0; if (isSuccess) { data = (JSONObject) resultMap.get("data"); addressInfo = JSON.parseObject(resultMap.get("data").toString(), IPAddressInfo.class); addressInfo.setAreaId(data.getInteger("area_id")) .setCityId(data.getInteger("city_id")) .setCountryId(data.getString("country_id")) .setCountyId(data.getInteger("county_id")) .setIspId(data.getInteger("isp_id")) .setRegionId(data.getInteger("region_id")); StringBuffer buffer = new StringBuffer(); buffer.append("[") .append(addressInfo.getIp()) .append("]") .append(" 位于 ") .append(addressInfo.getCountryId()) .append(" - ") .append(addressInfo.getCountry()) .append("[") .append(addressInfo.getArea()) .append("] - ") .append(addressInfo.getRegion()) .append(" - ") .append(addressInfo.getCity()) .append(" - ") .append(addressInfo.getCounty()) .append(" / 使用[") .append(addressInfo.getIsp()) .append("]网络"); addressInfo.setDescribe(buffer.toString()); } else { addressInfo = null; } result.setSuccess(isSuccess); result.setIpAddressInfo(addressInfo); return result; } }

2.结果类

public class TaobaoIPLibResult {

    private boolean isSuccess;
    private IPAddressInfo ipAddressInfo;

    public boolean isSuccess() {
        return isSuccess;
    }

    public void setSuccess(boolean success) {
        isSuccess = success;
    }

    public IPAddressInfo getIpAddressInfo() {
        return ipAddressInfo;
    }

    public void setIpAddressInfo(IPAddressInfo ipAddressInfo) {
        this.ipAddressInfo = ipAddressInfo;
    }
}

3.IP信息类

public class IPAddressInfo {


    private String ip;
    private String country;
    private String area;
    private String region;
    private String city;
    private String county;
    private String isp;
    private String countryId;
    private Integer areaId;
    private Integer regionId;
    private Integer cityId;
    private Integer countyId;
    private Integer ispId;
    private String describe;

    public String getDescribe() {
        return describe;
    }

    public IPAddressInfo setDescribe(String describe) {
        this.describe = describe;
        return this;
    }

    public String getIp() {
        return ip;
    }

    public IPAddressInfo setIp(String ip) {
        this.ip = ip;
        return this;
    }

    public String getCountry() {
        return country;
    }

    public IPAddressInfo setCountry(String country) {
        this.country = country;
        return this;
    }

    public String getArea() {
        return area;
    }

    public IPAddressInfo setArea(String area) {
        this.area = area;
        return this;
    }

    public String getRegion() {
        return region;
    }

    public IPAddressInfo setRegion(String region) {
        this.region = region;
        return this;
    }

    public String getCity() {
        return city;
    }

    public IPAddressInfo setCity(String city) {
        this.city = city;
        return this;
    }

    public String getCounty() {
        return county;
    }

    public IPAddressInfo setCounty(String county) {
        this.county = county;
        return this;
    }

    public String getIsp() {
        return isp;
    }

    public IPAddressInfo setIsp(String isp) {
        this.isp = isp;
        return this;
    }

    public String getCountryId() {
        return countryId;
    }

    public IPAddressInfo setCountryId(String countryId) {
        this.countryId = countryId;
        return this;
    }

    public Integer getAreaId() {
        return areaId;
    }

    public IPAddressInfo setAreaId(Integer areaId) {
        this.areaId = areaId;
        return this;
    }

    public Integer getRegionId() {
        return regionId;
    }

    public IPAddressInfo setRegionId(Integer regionId) {
        this.regionId = regionId;
        return this;
    }

    public Integer getCityId() {
        return cityId;
    }

    public IPAddressInfo setCityId(Integer cityId) {
        this.cityId = cityId;
        return this;
    }

    public Integer getCountyId() {
        return countyId;
    }

    public IPAddressInfo setCountyId(Integer countyId) {
        this.countyId = countyId;
        return this;
    }

    public Integer getIspId() {
        return ispId;
    }

    public IPAddressInfo setIspId(Integer ispId) {
        this.ispId = ispId;
        return this;
    }
}

你可能感兴趣的:(#,工具类)