springboot项目中集成ip2region遇到的问题及终极解决办法

1、问题回顾

按照ip2region项目的官方集成到springboot项目后,运行测试一切都ok,没有任何问题。但是当项目打成可执行的jar包后再运行,却显示找不到ip2region.db,无法找到资源文件的错误。异常代码如下:

java.io.FileNotFoundException: class path resource [ip2region/ip2region.db] cannot be resolved to absolute file path because it does not reside in the file system: jar:file:/app.jar!/BOOT-INF/classes!/ip2region/ip2region.db

2、解决办法

将ip2region.db文件放到本地目录,然后直接读取本地文件进行ip解析,代码如下:

1public static String getCityInfo(String ip) { 2 DbSearcher searcher = null; 3 try { 4 String dbPath = GlobalConfig.getProfile() + "ip2region/ip2region.db"; 5 //String dbPath = "f:/profile/ip2region/ip2region.db"; //本地测试使用地址 6 File file = new File(dbPath); 7 if (file.exists()) { 8 DbConfig config = new DbConfig(); 9 searcher = new DbSearcher(config, file.getPath()); 10 Method method = searcher.getClass().getMethod("btreeSearch", String.class); 11 if (!Util.isIpAddress(ip)) { 12 log.error("Error: Invalid ip address"); 13 } 14 DataBlock dataBlock = (DataBlock) method.invoke(searcher, ip); 15 return dataBlock.getRegion(); 16 } 17 } catch (Exception e) { 18 log.error("获取地址信息异常:{}", e.getLocalizedMessage()); 19 } 20 return "XX XX"; 21}

注:GlobalConfig.getProfile()是配置的本地文件存储路径

代码地址:[代码下载]

点赞
收藏

评论区

加载中...

相关推荐

MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1

文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

2020年前端实用代码段,为你的工作保驾护航

有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )

SpringBoot自定义序列化的使用方式

场景及需求:项目接入了SpringBoot开发,现在需求是服务端接口返回的字段如果为空,那么自动转为空字符串。例如:\    {        "id":1,        "name":null    },    {        "id":2,        "name":"x

Android So动态加载 优雅实现与原理分析

背景:漫品Android客户端集成适配转换功能(基于目标识别(So库35M)和人脸识别库(5M)),导致apk体积50M左右,为优化客户端体验,决定实现So文件动态加载.!(https://oscimg.oschina.net/oscnet/00d1ff90e4b34869664fef59e3ec3fdd20b.png)点击上方“蓝字”关注我