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解析,代码如下:
public static String getCityInfo(String ip) {
DbSearcher searcher = null;
try {
String dbPath = Profile() + "ip2region/ip2region.db";
//String dbPath = "f:/profile/ip2region/ip2region.db"; //本地测试使用地址
File file = new File(dbPath);
if (ists()) {
DbConfig config = new DbConfig();
searcher = new DbSearcher(config, Path());
Method method = Class().getMethod("btreeSearch", String.class);
if (!Util.isIpAddress(ip)) {
<("Error: Invalid ip address");}
DataBlock dataBlock = (DataBlock) method.invoke(searcher, ip);
Region();
}
} catch (Exception e) {
<("获取地址信息异常:{}", e.getLocalizedMessage());}
return "XX XX";
}
注Profile()是配置的本地文件存储路径
本文发布于:2024-01-30 21:05:12,感谢您对本站的认可!
本文链接:https://www.4u4v.net/it/170661991222836.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
留言与评论(共有 0 条评论) |