springboot專案中整合ip2region包 FileNotFoundException
阿新 • • 發佈:2018-12-11
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 ""; }