美文网首页
springboot免费的IP定位服务

springboot免费的IP定位服务

作者: neko_11 | 来源:发表于2020-01-16 20:57 被阅读0次

简介

ip2region 是准确率99.9%的ip地址定位库,0.0x毫秒级查询,数据库文件大小只有1.5M,提供了java,php,c,python,nodejs,golang,c#查询绑定和Binary,B树,内存三种查询算法!

maven依赖

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

DB文件下载地址:
https://github.com/lionsoul2014/ip2region/archive/v1.9.0-release.tar.gz

下载这个项目之后到data/文件夹下面找到ip2region.db,我放在了/user/home目录下

Java 工具类

public class IpUtil {

    public static String getCityInfo(String ip, int type) {
        //db
        String dbPath = "/usr/home/ip2region.db";
        File file = new File(dbPath);
        if (!file.exists()) {
            return "Error: Invalid ip2region.db file";
        }
        // 查询算法
        // DbSearcher.BTREE_ALGORITHM B-tree
        // DbSearcher.BINARY_ALGORITHM Binary
        // DbSearcher.MEMORY_ALGORITYM Memory
        // int algorithm = DbSearcher.BINARY_ALGORITHM;
        try {
            DbConfig config = new DbConfig();
            DbSearcher searcher = new DbSearcher(config, dbPath);
            //define the method
            Method method = null;
            switch (type) {
                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:
                    break;
            }
            DataBlock dataBlock;
            if (!Util.isIpAddress(ip)) {
                return "Error: Invalid ip address";
            }
            dataBlock = (DataBlock) method.invoke(searcher, ip);
            return dataBlock.getRegion();
        } catch (Exception e) {
            e.printStackTrace();
            return "exception";
        }
    }
}

写个接口用于测试

    @ApiOperation(value = "IP定位")
    @GetMapping("/test")
    public ResponseEntity<String> test(String ip, int type) {
        return ResponseEntity.ok(IpUtil.getCityInfo(ip, type));
    }

请求接口,定位成功:


image.png

相关文章

  • springboot免费的IP定位服务

    简介 ip2region 是准确率99.9%的ip地址定位库,0.0x毫秒级查询,数据库文件大小只有1.5M,提供...

  • android开发IP定位的实现

    百度地图API SDK之Web服务API 服务介绍 普通IP定位 普通IP定位是一套以HTTP/HTTPS形式提供...

  • 换ip软件的价格选择

    网络中的代理服务器可以分为免费跟付费的,用户选择换ip软件都是有各自不同的预算范围的。 使用免费代理IP,这种IP...

  • 关于网络地图(高德)定位的额额额。。。

    因为原生接口成功率很低,JS-API会优先调用精确IP定位服务,在IP定位失败的时候,尝试使用浏览器原生定位接口进...

  • 运营规划的维度

    运营规划的逻辑: 1、商业逻辑 买产品或服务; 免费+增值服务;(更贴近我们的定位) 免费+流量 or 数据变现 ...

  • js免费ip地址定位

    免费接口地址:http://ip.chinaz.com/getip.aspx调用示例: 搜狐新浪的: 返回参数比较...

  • RXJava 应用系列-场景2

    二、获取定位信息 场景描述: 首先使用GPS定位,如果定位失败使用IP定位从服务器获取。 问题分析: 此问题可以分...

  • 使用nginx设置代理服务器

    用爬虫过程中,如果用同一IP请求过多,会被服务端屏蔽,这时可以去网站上如 西刺免费代理IP 找一些免费IP代理,如...

  • 代理ip软件哪个便宜稳定

    代理IP需要服务器一大笔费用和人工支持,免费的代理ip是无法生存的。 免费的代理IP都来源于互联网采集,安全性、可...

  • IDEA远程连接springboot项目进行debug测试

    获取远程项目的 IP和端口 (确保远程服务是开启debug模式的,如果是springboot通过jar或者war方...

网友评论

      本文标题:springboot免费的IP定位服务

      本文链接:https://www.haomeiwen.com/subject/qqdqzctx.html