springboot專案中整合ip2region包 FileNotFoundException
05:15:55.344 sms [asyncTaskExecutor-1] ERROR cc.mrbird.common.util.AddressUtils 54 - 獲取地址資訊異常:{}
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
at org.springframework.util.ResourceUtils.getFile(ResourceUtils.java:217)
at org.springframework.core.io.AbstractFileResolvingResource.getFile(AbstractFileResolvingResource.java:133)
at cc.mrbird.common.util.AddressUtils.getCityInfo(AddressUtils.java:25)
at cc.mrbird.system.service.impl.LogServiceImpl.saveLog(LogServiceImpl.java:94)
at cc.mrbird.system.service.impl.LogServiceImpl$$FastClassBySpringCGLIB$$f6e62249.invoke(<generated>)
at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:746)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163)
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:294)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:98)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185)
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:688)
at cc.mrbird.system.service.impl.LogServiceImpl$$EnhancerBySpringCGLIB$$3e5323c8.saveLog(<generated>)
at cc.mrbird.system.service.impl.LogServiceImpl$$FastClassBySpringCGLIB$$f6e62249.invoke(<generated>)
at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:746)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163)
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:294)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:98)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185)
at org.springframework.aop.interceptor.AsyncExecutionInterceptor.lambda$invoke$0(AsyncExecutionInterceptor.java:115)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)
public static String getCityInfo(String ip) {
try {
// Resource resource = new ClassPathResource("ip2region/ip2region.db");
// File file = resource.getFile();
// if (!file.exists()) {
//
// }
String dbPath = AddressUtils.class.getResource("/ip2region/ip2region.db").getPath();
File file = new File(dbPath);
if (file.exists() == false) {
String tmpDir = System.getProperties().getProperty("java.io.tmpdir");
dbPath = tmpDir + "ip.db";
System.out.println(dbPath);
file = new File(dbPath);
FileUtils.copyInputStreamToFile(AddressUtils.class.getClassLoader().getResourceAsStream("classpath:ip2region/ip2region.db"), file);
}
int algorithm = DbSearcher.BTREE_ALGORITHM;
DbConfig config = new DbConfig();
DbSearcher searcher = new DbSearcher(config, file.getPath());
Method method = null;
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:
method = searcher.getClass().getMethod("memorySearch", String.class);
break;
}
DataBlock dataBlock = null;
if (!Util.isIpAddress(ip)) {
log.error("Error: Invalid ip address");
}
dataBlock = (DataBlock) method.invoke(searcher, ip);
return dataBlock.getRegion();
} catch (Exception e) {
log.error("獲取地址資訊異常:{}", e);
}
return "";
}
相關文章
- springboot專案整合mybatisSpring BootMyBatis
- SpringBoot專案整合RabbitMQSpring BootMQ
- SpringBoot專案整合日誌Spring Boot
- SpringBoot專案整合MinIOSpring Boot
- Springboot專案中需整合的依賴集和檔案配置Spring Boot
- springboot專案打war包流程Spring Boot
- SpringBoot + Mybatis + Redis 整合入門專案Spring BootMyBatisRedis
- Springboot2+vue2整合專案Spring BootVue
- Laravel 專案實戰中如何快速整合 Emoji 表情包?Emoji 表情包太豐富了Laravel
- springboot專案中yaml檔案Spring BootYAML
- 現有專案中整合FlutterFlutter
- 【SpringBoot】三十二、SpringBoot專案Jar包如何瘦身部署Spring BootJAR
- SpringBoot專案整合阿里Druid連線池Spring Boot阿里UI
- springboot+vue專案如何整合企業微信Spring BootVue
- 整合專案中的風險管理 (轉)
- Springboot 獲取jar包中的檔案Spring BootJAR
- springboot系列學習(十九):springboot專案整合Druid,Druid到底是什麼,他是在專案中如何使用的Spring BootUI
- 替換專案中的包名
- iOS原生專案中整合flutter初試iOSFlutter
- 在Android原生專案中整合FlutterAndroidFlutter
- Flowable與springBoot專案整合及出現的問題Spring Boot
- springboot專案整合druid資料庫連線池Spring BootUI資料庫
- 將zookeeper curator與springboot專案進行整合(重點)Spring Boot
- SpringBoot專案中遇到的異常Spring Boot
- 從Maven專案中獲取Jar包MavenJAR
- springboot 整合外部tomcat war包部署方式Spring BootTomcat
- 專案整合管理
- RN學習(四)——RN專案整合到現有iOS專案中iOS
- Flutter在混合專案中的構建和整合Flutter
- 在前後端分離專案中使用SpringBoot整合Shiro後端Spring Boot
- SpringBoot專案配置檔案中密碼的加密Spring Boot密碼加密
- springboot專案中的異常處理Spring Boot
- 如何刪除springboot中的子專案Spring Boot
- 【Saas-export專案】--專案整合(實體類、整合mybatis、service)ExportMyBatis
- SpringBoot系列(十五)整合快取,專案會用得到的技術Spring Boot快取
- 【Java分享客棧】SpringBoot整合WebSocket+Stomp搭建群聊專案JavaSpring BootWeb
- Docker 部署 SpringBoot 專案整合 Redis 映象做訪問計數DemoDockerSpring BootRedis
- Docker部署SpringBoot專案整合Redis映象做訪問計數DemoDockerSpring BootRedis