Java 根据IP获取城市(ip2region)

本文介绍了疫情期间发现的抖音和快手新功能——IP属性查看,聚焦于一款名为ip2region的高效IP地址定位库,具有99.9%的准确率和0.0x毫秒级查询速度,通过内存、二分查找和B树算法实现。讲解了其特点、数据来源和Maven集成方式,并展示了使用示例。
摘要由CSDN通过智能技术生成

简介:

      1.疫情期间,大家注意防护哈

       2.今天看到了抖音和快手的新功能,可以看到IP属性,于是灵感立马闪现,出于好奇就打开电脑埋头苦研,去查各种资料。

Ip2region是什么?

    ip2region - 准确率99.9%的离线IP地址定位库,0.0x毫秒级查询,ip2region.db数据库只有数MB,提供了java,php,c,python,nodejs,golang,c#等查询绑定和Binary,B树,内存三种查询算法。 

Ip2region特性

1.99.9%准确率

2.数据聚合了一些知名ip到地名查询提供商的数据,这些是他们官方的的准确率,经测试着实比经典的纯真IP定位准确一些。
3.ip2region的数据聚合自以下服务商的开放API或者数据(升级程序每秒请求次数2到4次):

4.ps:如果上述开放API或者数据都不给开放数据时ip2region将停止数据的更新服务。

标准化的数据格式

每条ip数据段都固定了格式:

        城市Id|国家|区域|省份|城市|ISP

查询速度快
全部的查询客户端单次查询都在0.x毫秒级别,内置了三种查询算法:

1.memory算法:整个数据库全部载入内存,单次查询都在0.1x毫秒内,C语言的客户端单次查询在0.00x毫秒级别。
2.binary算法:基于二分查找,基于ip2region.db文件,不需要载入内存,单次查询在0.x毫秒级别。
3.b-tree算法:基于btree算法,基于ip2region.db文件,不需要载入内存,单词查询在0.x毫秒级别,比binary算法更快。
ps:任何客户端b-tree都比binary算法快,当然memory算法固然是最快的!
 

 

maven集成:

    1.引入依赖: 

<dependencies>
        <dependency>
            <groupId>org.lionsoul</groupId>
            <artifactId>ip2region</artifactId>
            <version>1.7.2</version>
        </dependency>
    </dependencies>

2.下载ip2region.db 

3.将ip2region.db放入项目中的src/main/resource文件夹下

4.精彩的内容到了,就开始了代码  

package utils;

import org.lionsoul.ip2region.DataBlock;
import org.lionsoul.ip2region.DbConfig;
import org.lionsoul.ip2region.DbSearcher;
import org.lionsoul.ip2region.Util;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.Method;
import java.net.URL;

public class IPUtil {

    /**
     * 根据IP地址获取城市
     * @param ip
     * 如果放在服务器读取不了,两种方式
     * 1.可以在服务器上创建文件,ip2region.db这个放在文件里面,然后开始读取
     * 2.可以整个配置文件,在配置配置目录,然后读取配置文件
     * @return
     */
    public static String getCityInfo(String ip) throws IOException {
        URL url = IPUtil.class.getClassLoader().getResource("ip2region.db");
        File file;
        if (url != null) {
            file = new File(url.getFile());
        } else {
            return null;
        }
        if (!file.exists()) {
            System.out.println("Error: Invalid ip2region.db file, filePath:" + file.getPath());
            return null;
        }
        //查询算法
        int algorithm = DbSearcher.BTREE_ALGORITHM; //B-tree
        //DbSearcher.BINARY_ALGORITHM //Binary
        //DbSearcher.MEMORY_ALGORITYM //Memory
        try {
            DbConfig config = new DbConfig();
            DbSearcher searcher = new DbSearcher(config, file.getPath());
            Method method;
            switch ( algorithm )
            {
                case DbSearcher.BTREE_ALGORITHM:
                    method = searcher.getClass().getMethod("btreeSearch", String.class);
                    break;
                case DbSearcher.BINARY_ALGORITHM:
                    method = searcher.getClass().getMethod("binarySearch", String.class);
                    break;
                case DbSearcher.MEMORY_ALGORITYM:
                    method = searcher.getClass().getMethod("memorySearch", String.class);
                    break;
                default:
                    return null;
            }
            DataBlock dataBlock;
            if (!Util.isIpAddress(ip)) {
                System.out.println("Error: Invalid ip address");
                return null;
            }
            dataBlock  = (DataBlock) method.invoke(searcher, ip);
            return dataBlock.getRegion();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

}

 

package ceshi;

import utils.IPUtil;

import java.io.IOException;
import java.net.InetAddress;


public class CeShiIp {

    public static void main(String[] args) throws IOException {
        //获取IP地址
        String hostAddress = InetAddress.getLocalHost().getHostAddress();
        //工具类
        String detail = IPUtil.getCityInfo(hostAddress);
        System.out.println("IP地址:"+hostAddress+"地区:"+detail);
    }
}

 

 

5.效果: IP就不给大家展示了

      IP地址:xxx 地区:中国|0|北京|北京市|联通

 

 

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值