根据IP地址获取地理位置的工具类(文中IP库为IPPlus360库)

前言

提示:工作中总结了根据IP地址获取地理位置的工具类,有需要的小伙伴可供参考(注:我使用的IP库为IPPlus360库)

代码如下:

import com.bytefen.entity.IPAddressEntity;
import com.bytefen.util.mapper.JsonMapper;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.JsonNode;
import com.ipplus360.db.AWReader;

import java.io.File;
import java.net.InetAddress;
import java.util.List;
import java.util.Map;

/**
 * @ClassName: IPPlus360Util
 * @Author: Binean
 */

public class IPPlus360Util {

    public static IPAddressEntity finIPGeo(String ipAddress) {
        IPAddressEntity info = new IPAddressEntity();
        try {ip
            String ip = ipAddress;
            String ipipDBFile = PropertyUtil.getProperty("ipplus360库文件路径");
            String filePath = ipipDBFile;
            AWReader awReader = new AWReader(new File(filePath));
            InetAddress address = InetAddress.getByName(ip);
            JsonNode jsonNode = awReader.get(address);

            JsonMapper mapper = JsonMapper.nonEmptyMapper();
            String string = mapper.toJson(jsonNode);
            JavaType javaType = mapper.contructMapType(Map.class, String.class, Object.class);
            Map<String, Object> map = mapper.fromJson(string, javaType);
            String country = StringUtil.trim(map.get("country"));
            String isp = StringUtil.trim(map.get("owner"));
            String newIsp = null;
            byte[] bytes = isp.getBytes();
            int i = bytes.length;//i为字节长度
            int j = isp.length();//j为字符长度
            if (i != j) {
                newIsp = isp.substring(isp.length() - 2);
            } else {
                newIsp = isp;
            }
            List<Map<String, Object>> multiAreas = (List<Map<String, Object>>) map.get("multiAreas");
            String newProv = null;
            String newCity = null;
            for (Map<String, Object> multiArea : multiAreas) {
                String prov = StringUtil.trim(multiArea.get("prov"));
                if (!StringUtil.isEmpty(prov)) {
                    newProv = prov.substring(0, prov.length() - 1);
                }
                String city = StringUtil.trim(multiArea.get("city"));
                if (!StringUtil.isEmpty(prov)) {
                    newCity = city.substring(0, city.length() - 1);
                }
            }

            info.setCountry(country);
            info.setProv(newProv);
            info.setCity(newCity);
            info.setIsp(newIsp);

            return info;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return info;
    }

    public static String findGEOAddress(String ipAddress) {
        if (IPPlus360Util.isIP(ipAddress)) {
            IPAddressEntity ipAddressEntity = IPPlus360Util.finIPGeo(ipAddress);
            if (ipAddressEntity == null) {
                return null;
            }
            String country = ipAddressEntity.getCountry();
            String regionCity = (ipAddressEntity.getProv() == null) ? "" : ipAddressEntity.getProv();
            if (!StringUtil.isEmpty(ipAddressEntity.getCity()) && !regionCity.equals(ipAddressEntity.getCity())) {
                regionCity += " " + ipAddressEntity.getCity();
            }
            String area = country;
            if (!StringUtil.isEmpty(regionCity) && !area.equals(regionCity)) {
                area += " " + regionCity;
            }
            String isp = StringUtil.isEmpty(ipAddressEntity.getIsp()) ? null : ipAddressEntity.getIsp();
            if (isp != null) {
                area += " " + isp;
            }
            return area;
        } else {
            return null;
        }
    }

    //判断输入字符串是否为IP的方法
    public static boolean isIP(String str){
		if(StringUtil.isEmpty(str)){
			return false;
		}
		String p = "^(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|[0-9])\\."
				+"(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)\\."
				+"(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)\\."
				+"(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)$";
		Pattern pattern = Pattern.compile(p);
		return pattern.matcher(str).matches();
	}
}

下面附上返回参数接收实体类(IPAddressEntity.Java):

/**
 *@author Binean
 *IPPlus360返回参数接收实体类
 */
public class IPAddressEntity {
	private String country;
	private String prov;
	private String city;
	private String isp;

	public String getCountry() {
		return country;
	}

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

	public String getProv() {
		return prov;
	}

	public void setProv(String prov) {
		this.prov = prov;
	}

	public String getCity() {
		return city;
	}

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

	public String getIsp() {
		return isp;
	}

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

	@Override
	public String toString() {
		return "IPAddressEntity{" +
				"country='" + country + '\'' +
				", prov='" + prov + '\'' +
				", city='" + city + '\'' +
				", isp='" + isp + '\'' +
				'}';
	}
}


提示:这里pom文件需要导入com.ipplus360.db的jar包
在这里插入图片描述
jar包下载地址:
https://gitee.com/shao_dong/com/raw/master/com/ipplus360/db/awdb/0.0.1-SNAPSHOT/awdb-0.0.1-SNAPSHOT.jar

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值