通过Ip查询对应地址,Ip2location全球IP地址网段

通过Ip查询对应地址,Ip2location全球IP地址网段

1. Ip2location介绍

Ip2location IP 库 是比较准确的 在 免费查询IP 行列, 有很多的地址段, 有 Ip 详情等级 如 国家, 国家 城市, 国家 城市 经纬度, 邮编等 , 已做过大量IP 测试 测试数据
IP测试数据全球IP1000个
测试结果

未查询到的IP数量:0
查询到的国家-------------------------------
国家=Myanmar,数量=1258
国家=Japan,数量=1
国家=China,数量=4
国家=Malaysia,数量=1
国家=Thailand,数量=1
查询到的城市-------------------------------
城市=Shwebo,数量=21
城市=Thayetmyo,数量=2
城市=Pyay,数量=12
城市=Thanatpin,数量=10
城市=Haka,数量=14
城市=Pakokku,数量=1
城市=Meiktila,数量=25
城市=Taunggyi,数量=22
城市=Hpa-An,数量=9
城市=Yamethin,数量=11
城市=Skudai,数量=1
城市=Mandalay,数量=88
城市=Kamphaeng Phet,数量=1
城市=Shenzhen,数量=4
城市=Monywa,数量=25
城市=Yangon,数量=508
城市=Mawlamyinegyunn,数量=7
城市=Syriam,数量=40
城市=Pathein,数量=9
城市=Maymyo,数量=20
城市=Bogale,数量=6
城市=Lashio,数量=336
城市=Pyu,数量=33
城市=Magway,数量=31
城市=Mogok,数量=8
城市=Nay Pyi Taw,数量=20
城市=Niigata,数量=1
2. 使用

准备资料: 需要能够FQ
官网 : LINK
国家 城市 ip : LINK
更多请参考
官网下载资料
下载对应的全球IP段文件

3.java代码解析

public class IPIntervalStoreBean {

    // 开始 IP
    private String ip;
    // 国家简单代码
    private String countrySim;
    // 国家名称
    private String country;
    // 城市名称
    private String city;
    // 经度
    private long lng;
    // 纬度
    private long lat;


    public IPIntervalStoreBean(String ip, String countrySim, String country, String city,long lng,long lat) {
        this.ip = ip;
        this.countrySim = countrySim;
        this.country = country;
        this.city = city;
        this.lng = lng;
        this.lat = lat;
    }
    public IPIntervalStoreBean() {
    }


    public static String toLogStoreString(IPIntervalStoreBean ipBean) {
        StringBuffer res = new StringBuffer();
        if (ipBean == null) {
            res.append("ip=").append("^")
               .append("countrySim=").append("^")
               .append("country=").append("^")
               .append("city=").append("^")
               .append("lng=").append("^")
               .append("lat=").append("^");
        }else{
            res.append("ip=").append(ipBean.getIp()).append("^")
                    .append("countrySim=").append(ipBean.getCountrySim()).append("^")
                    .append("country=").append(ipBean.getCountry()).append("^")
                    .append("city=").append(ipBean.getCity()).append("^")
                    .append("lng=").append(ipBean.getLng()).append("^")
                    .append("lat=").append(ipBean.getLat()).append("^");
        }
        return res.toString();
    }

    public static IPIntervalStoreBean parseStringToObj(String str) {
        if (StringUtils.isBlank(str)) {
            return null;
        }
        String ip = MpaasUtils.getPageWaitTime(str, "ip");
        String countrySim = MpaasUtils.getPageWaitTime(str, "countrySim");
        String country = MpaasUtils.getPageWaitTime(str, "country");
        String city = MpaasUtils.getPageWaitTime(str, "city");
        String lng = MpaasUtils.getPageWaitTime(str, "lng");
        String lat = MpaasUtils.getPageWaitTime(str, "lat");
        if (StringUtils.isBlank(lng) || StringUtils.isBlank(lat)) {
            lng = "0";
            lat = "0";
        }
        IPIntervalStoreBean res = new IPIntervalStoreBean(ip,countrySim,country,city,Long.parseLong(lng),Long.parseLong(lat));
        return res;
    }


    public String getIp() {
        return ip;
    }

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

    public String getCountrySim() {
        return countrySim;
    }

    public void setCountrySim(String countrySim) {
        this.countrySim = countrySim;
    }

    public String getCountry() {
        return country;
    }

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

    public String getCity() {
        return city;
    }

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

    public long getLng() {
        return lng;
    }

    public void setLng(long lng) {
        this.lng = lng;
    }

    public long getLat() {
        return lat;
    }

    public void setLat(long lat) {
        this.lat = lat;
    }
}

工具类

/**
 * @Description 查询 IP 地址 的 Utils 工具类
 * @Author sunyalong
 * @Date 2020/8/6 14:24
 */
public class IpFindUtils {


    /**
     * 通过ip 查询这个ip 所在的地址
     *
     * @param ip
     * @param ipDataList
     * @return 如果没有查询到 ip 地址 返回为 null
     */
    public static IPIntervalStoreBean findAddressByIp(String ip, List<IPIntervalBean> ipDataList) {
        if (StringUtils.isBlank(ip)) {
            return null;
        }

        IPIntervalBean ipIntervalBean = null;
        long iplong = ipToLong(ip);
        for (IPIntervalBean ipBean3 : ipDataList) {
            if (iplong >= ipBean3.getIpStart() && iplong <= ipBean3.getIpEnd()) {
                ipIntervalBean =  ipBean3;
                break;
            }
        }

        if (ipIntervalBean == null) {
            return null;
        }
        IPIntervalStoreBean ipIntervalStoreBean = new IPIntervalStoreBean(ip, ipIntervalBean.getCountrySimple(),
                ipIntervalBean.getCountry(), ipIntervalBean.getCity(),ipIntervalBean.getLongitude(),ipIntervalBean.getLatitude());
        return ipIntervalStoreBean;
    }

    /**
     * ip 转为 long
     *
     * @param ipAddress
     * @return
     */
    public static long ipToLong(String ipAddress) {
        long result = 0;
        String[] ipAddressInArray = ipAddress.split("\\.");
        for (int i = 3; i >= 0; i--) {
            long ip = Long.parseLong(ipAddressInArray[3 - i]);
            result |= ip << (i * 8);
        }

        return result;
    }

    /**
     * long 转换为 IP
     * @param ip
     * @return
     */
    public static String longToIp(long ip) {
        return ((ip >> 24) & 0xFF) + "."
                + ((ip >> 16) & 0xFF) + "."
                + ((ip >> 8) & 0xFF) + "."
                + (ip & 0xFF);
    }


    public static List<IPIntervalBean> getCSVFileIpSourceData(InputStream ins) throws IOException {
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(ins, "UTF-8"));
        List<String[]> strings = readData(bufferedReader);
        ArrayList<IPIntervalBean> allIpList = new ArrayList<>();
        for (String[] datum : strings) {
            String ipStart = datum[0].trim().replace("\"","");
            String ipEnd = datum[1].trim().replace("\"","");;
            String countrySimple = datum[2].replace("\"","");;
            String country = datum[3].replace("\"","");;
            String city = datum[4].replace("\"","");;
            IPIntervalBean ipBean = new IPIntervalBean(Long.parseLong(ipStart), Long.parseLong(ipEnd), countrySimple, country, city);
            allIpList.add(ipBean);
        }
        return allIpList;
    }
    /**
     * 读取一个 csv 文件 的 ip 地址 到 List 集合
     *
     * @param csvFilePath
     * @return
     * @throws IOException
     */
    public static List<IPIntervalBean> getCSVFileIpSourceData(String csvFilePath) throws IOException {
        List<String[]> csvDataList = getExcelData(csvFilePath);
        // 所有 IP加密原数据
        ArrayList<IPIntervalBean> allIpList = new ArrayList<>();
        for (String[] datum : csvDataList) {
            String ipStart = datum[0].trim().replace("\"","");
            String ipEnd = datum[1].trim().replace("\"","");;
            String countrySimple = datum[2].replace("\"","");;
            String country = datum[3].replace("\"","");;
            String city = datum[4].replace("\"","");;
            IPIntervalBean ipBean = new IPIntervalBean(Long.parseLong(ipStart), Long.parseLong(ipEnd), countrySimple, country, city);
            allIpList.add(ipBean);
        }
        return allIpList;
    }


    /**
     * 读取一个 excel 文件
     *
     * @param fileName
     * @return
     * @throws IOException
     */
    public static List<String[]> getExcelData(String fileName) throws IOException {
        // 设定UTF-8字符集,使用带缓冲区的字符输入流BufferedReader读取文件内容
        BufferedReader file = new BufferedReader(new InputStreamReader(new FileInputStream(fileName), "UTF-8"));
        return readData(file);
    }

    private static List<String[]> readData(BufferedReader file) throws IOException {
        List<String[]> records = new ArrayList<String[]>();
        String record;
        // 遍历数据行并存储在名为records的ArrayList中,每一行records中存储的对象为一个String数组
        while ((record = file.readLine()) != null) {
            String fields[] = record.split(",");
            records.add(fields);
        }
        // 关闭文件
        file.close();
        return records;
    }
}

使用方法

public class IPTest {
		// 下载的 IP段文件
		private static String CSV_FILE_PATH = "D:\\workspace\\code\\tools\\mdap\\log-gateway\\src\\main\\resources\\IP2LOCATION-LITE-DB3.CSV";
		public static void main(String[] args) throws IOException {
				String youIp = "";
        // 获得 ip 源数据
        List<IPIntervalBean> csvFileIpSourceData = IpFindUtils.getCSVFileIpSourceData(CSV_FILE_PATH);

      	IPIntervalStoreBean addressByIp = IpFindUtils.findAddressByIp(youIp, csvFileIpSourceData);
			
    }
}

如果无法FQ下载不了文件可以点击这个链接,我为大家下载好了LINK

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值