blank
blank
发布于 2019-09-20 / 423 阅读 / 0 评论 / 0 点赞

可执行 SpringBoot Jar 包 读取资源文件出错解决

在 IDEA 中运行是正常的 一旦打包成 jar 包(发布到 Docker 中)运行 则会出错

当项目是以 Jar 包运行时 如果使用 File 对象 来读取 Jar 包中的资源会导致找不到对应资源的错误

    /**
     * 读取 ip 数据库文件
     *
     * @param inetAddress InetAddress
     * @return com.maxmind.geoip2.model.CityResponse
     * @throws IOException
     * @throws GeoIp2Exception
     * @author blank
     * @date 2019-9-19 上午 9:47
     */
    private static CityResponse getCityResponse(InetAddress inetAddress) throws IOException, GeoIp2Exception {

        File file = ResourceUtils.getFile("classpath:GeoLite2-City.mmdb");
//        File file = new ClassPathResource("classpath:GeoLite2-City.mmdb").getFile();
//        File file = new ClassPathXmlApplicationContext().getResource("classpath:GeoLite2-City.mmdb").getFile();
//        File file = ResourceLoader.getResource("classpath:test-environment.yml").getFile();
        DatabaseReader reader = new DatabaseReader.Builder(file).build();
        if (internalIp(inetAddress)) {
            return null;
        }
        return reader.city(inetAddress);
    }

ip 数据库文件存放位置如图

image.png

无法读取 ip 数据库文件导致返回了 null

image.png

java.io.FileNotFoundException: class path resource [GeoLite2-City.mmdb] cannot be resolved to absolute file path because it does not reside in the file system: jar:file:/app.jar!/BOOT-INF/lib/common-1.0.jar!/GeoLite2-City.mmdb
	at org.springframework.util.ResourceUtils.getFile(ResourceUtils.java:217)
	at org.springframework.util.ResourceUtils.getFile(ResourceUtils.java:180)
	at com.blankhang.common.until.IpUtil.getCityResponse(IpUtil.java:30)
	at com.blankhang.common.until.IpUtil.getCountryCn(IpUtil.java:111)
	at com.blankhang.common.until.IpUtil.getCountryCn(IpUtil.java:137)
	at com.blankhang.controller.TestController.getIp(TestController.java:69)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:209)
	at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:136)
	at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:102)
	at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:877)
	at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:783)
	at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87)
	at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:991)
	at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:925)
	at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:974)
	at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:866)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:635)
	at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:851)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
	at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
	at org.springframework.boot.actuate.metrics.web.servlet.WebMvcMetricsFilter.filterAndRecordMetrics(WebMvcMetricsFilter.java:158)
	at org.springframework.boot.actuate.metrics.web.servlet.WebMvcMetricsFilter.filterAndRecordMetrics(WebMvcMetricsFilter.java:126)
	at org.springframework.boot.actuate.metrics.web.servlet.WebMvcMetricsFilter.doFilterInternal(WebMvcMetricsFilter.java:111)
	at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
	at org.springframework.boot.actuate.web.trace.servlet.HttpTraceFilter.doFilterInternal(HttpTraceFilter.java:90)
	at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
	at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:99)
	at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
	at org.springframework.web.filter.HttpPutFormContentFilter.doFilterInternal(HttpPutFormContentFilter.java:109)
	at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
	at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:93)
	at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
	at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:200)
	at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
	at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:198)
	at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
	at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:496)
	at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140)
	at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:81)
	at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)
	at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342)
	at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:803)
	at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
	at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:790)
	at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1468)
	at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
	at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
	at java.lang.Thread.run(Thread.java:748)

解决方法不使用 File 对象 使用 Resource 对象来读取资源文件

    /**
     * 读取 ip 数据库文件
     *
     * @param inetAddress InetAddress
     * @return com.maxmind.geoip2.model.CityResponse
     * @throws IOException
     * @throws GeoIp2Exception
     * @author blank
     * @date 2019-9-19 上午 9:47
     */
    private static CityResponse getCityResponse(InetAddress inetAddress) throws IOException, GeoIp2Exception {

//        File file = ResourceUtils.getFile("classpath:GeoLite2-City.mmdb");
//        File file = new ClassPathResource("classpath:GeoLite2-City.mmdb").getFile();
//        File file = new ClassPathXmlApplicationContext().getResource("classpath:GeoLite2-City.mmdb").getFile();
//        File file = ResourceLoader.getResource("classpath:test-environment.yml").getFile();
//        Resource resource = resourceLoader.getResource("classpath:GeoLite2-Country.mmdb");
        Resource resource = new ClassPathResource("GeoLite2-City.mmdb");
        DatabaseReader reader = new DatabaseReader.Builder(resource.getInputStream()).build();
        if (internalIp(inetAddress)) {
            return null;
        }
        return reader.city(inetAddress);
    }

解决之后 ip 数据文件能够正常读取解析并返回

image.png


评论