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專案整合MinIOSpring Boot
- springboot專案整合mybatisSpring BootMyBatis
- SpringBoot專案整合RabbitMQSpring BootMQ
- SpringBoot專案整合日誌Spring Boot
- springboot專案打war包流程Spring Boot
- Springboot專案中需整合的依賴集和檔案配置Spring Boot
- Springboot2+vue2整合專案Spring BootVue
- SpringBoot + Mybatis + Redis 整合入門專案Spring BootMyBatisRedis
- 【SpringBoot】三十二、SpringBoot專案Jar包如何瘦身部署Spring BootJAR
- SpringBoot專案整合阿里Druid連線池Spring Boot阿里UI
- springboot專案中yaml檔案Spring BootYAML
- springboot系列學習(十九):springboot專案整合Druid,Druid到底是什麼,他是在專案中如何使用的Spring BootUI
- springboot+vue專案如何整合企業微信Spring BootVue
- springboot專案整合druid資料庫連線池Spring BootUI資料庫
- Laravel 專案實戰中如何快速整合 Emoji 表情包?Emoji 表情包太豐富了Laravel
- Springboot 獲取jar包中的檔案Spring BootJAR
- 現有專案中整合FlutterFlutter
- springboot 整合外部tomcat war包部署方式Spring BootTomcat
- 將zookeeper curator與springboot專案進行整合(重點)Spring Boot
- springBoot專案部署整合阿里雲https證書報錯Spring Boot阿里HTTP
- 在前後端分離專案中使用SpringBoot整合Shiro後端Spring Boot
- Flowable與springBoot專案整合及出現的問題Spring Boot
- SpringBoot專案中遇到的異常Spring Boot
- 在Android原生專案中整合FlutterAndroidFlutter
- 【Java分享客棧】SpringBoot整合WebSocket+Stomp搭建群聊專案JavaSpring BootWeb
- 替換專案中的包名
- SpringBoot專案配置檔案中密碼的加密Spring Boot密碼加密
- SpringBoot專案在Eclipse/MyEclipse中執行Spring BootEclipse
- springboot專案中的異常處理Spring Boot
- 如何刪除springboot中的子專案Spring Boot
- iOS原生專案中整合flutter初試iOSFlutter
- SpringBoot系列(十五)整合快取,專案會用得到的技術Spring Boot快取
- SpringBoot專案搭建Spring Boot
- 專案整合管理
- RN學習(四)——RN專案整合到現有iOS專案中iOS
- IDEA+gradle將springBoot專案打可執行的Jar包IdeaGradleSpring BootJAR
- 在SpringBoot專案中整合SpringSession,基於Redis實現對Session的管理和事件監聽Spring BootGseSessionRedis事件
- Spring Data JPA系列2:SpringBoot整合JPA詳細教程,快速在專案中熟練使用JPASpring Boot