IP地址解析为省、市、区县

IP地址解析为省、市、区县


背景:网上有很多基于用户IP解析得到具体地址的应用,本项目扩展用户登录信息,标注用户所在地信息。

工具选择

要求:解析到区县级、免费、轻量。

IP2region

GitHub:https://github.com/zoujingli/ip2region
下载得到 ip2region_v4.xdb 文件,即IP离线库。
瑕疵:IP地址无法解析到区县级,目前测试只能获取地市。不满足我的需要
外网地址格式为:中国|浙江省|杭州市|电信
内网地址格式为:0|0|内网IP|内网IP
参考代码如下:

package org.utils;

import org.lionsoul.ip2region.xdb.Searcher;
import java.io.IOException;
import org.apache.commons.io.IOUtils;
import java.io.InputStream;
import java.net.URL;
import java.util.Objects;

public class Ip2Region {
    protected static Searcher searcher;
    //Todo 在奋斗中沸腾,于岁月里坚守
    public static Searcher getSearch(){
        InputStream resourceAsStream = Ip2Region.class.getClassLoader().getResourceAsStream("/ip2region_v4.xdb");
        if (resourceAsStream != null) {
            System.out.println("resource not null");
            byte [] cBuff= null;
            try {
                cBuff = IOUtils.toByteArray(resourceAsStream);
                searcher = Searcher.newWithBuffer(cBuff);
            }catch (IOException e) {
                e.printStackTrace();
            }finally {
                try {
                    resourceAsStream.close();
                }catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }else {
            System.out.println("resource null");
        }
        return searcher;
    }

    /**
     *
     * @param ip
     * @return 国家|区域|省份|城市
     */
    public static String getCityInfo(String ip){
        if (ip == null || ip.length() == 0) {
            return null;
        }
        try {
            String path = Objects.requireNonNull(Ip2Region.class.getResource("/ip2region_v4.xdb")).getPath();
            searcher = Searcher.newWithFileOnly(path);
            return searcher.search(ip);
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    public static void main(String[] args) throws Exception{
        // 记录开始时间
        String ip = "";
        String region = getCityInfo(ip);        
        System.out.println(region);
        // 中国|浙江省|杭州市|电信
    }
}

纯真IP库

由于IP2region无法解析到区县,故而又寻找新的IP库,纯真
纯真 (CZ88.net) 作为全球网络空间地理测绘技术的先行者,秉持开源精神和共享软件理念为中国互联网行业提供了高质量的网络空间地理数据库。纯真IP库创立至今已服务超过16万企业和软件开发者,在中国互联网发展历史上留下广泛的印迹。纯真IP库为中国所有后续百花齐放的IP地理信息数据产品提供了开源根基。
纯真IP库提供API、离线库方式,这里采用离线库的方式,版本为纯真社区版IP库
纯真社区版IP库以二进制(CZDB)的形式发布,配有开源的数据解析程序。该IP库文件同时支持IPv4和IPv6地理位置数据,具有占用内存小,访问速度快,线程安全,更新方便快捷的特点。

CZDB解析程序和使用文档已公开到GITHUB,以下为目前已支持的编程语言:
★ JAVA语言 https://github.com/tagphi/czdb-search-java
★ PHP语言 https://github.com/tagphi/czdb_searcher_php
★ C语言 https://github.com/tagphi/czdb-search-c
★ Node.js 语言解析程序 https://github.com/limkim0530/czdb-search-node
★ Python 语言解析程序 https://github.com/tagphi/czdb_searcher_python
★ GOLANG语言 https://github.com/tagphi/czdb-search-golang
★ C#语言 https://github.com/tagphi/czdb_searcher_csharp

可免费获取授权,要求如下:
在这里插入图片描述

具体使用待补充。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值