java.lang.IllegalArgumentException:URI不是分层的(在已部署的应用程序中)

时间:2018-03-11 16:37:27

标签: java tomcat jar war java-io

我已将我的应用程序部署到jar文件。我想在我的应用程序的资源文件夹中生成和存储PDF,Everything在开发中对我有用,但是当我部署时,我在实时应用程序中出错了

以下是我的代码

        URL url = this.getClass().getResource("/templates");
        parentDirectory = new File(new URI(url.toString()));
        OutputStream outputStream = new FileOutputStream(parentDirectory.toString()+"/"+quoteNumber+".pdf");

当我从Eclips运行我的应用程序但是我在服务器中部署应用程序WAR文件时,一切正常并且成功创建了PDf文件我得到了这个错误

我的错误完整日志正在关注

  

2018-03-09 18:23:31.033错误6 --- [XNIO-2任务-13]   o.z.p.spring.web.advice.AdviceTrait:内部服务器错误   java.lang.IllegalArgumentException:URI不是分层结构   java.io.File。(File.java:418)at   com.beequote.web.rest.QuotebuilderResource.Quote(QuotebuilderResource.java:338)     在   com.beequote.web.rest.QuotebuilderResource $$ FastClassBySpringCGLIB $$ 7c210806.invoke()     在   org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)     在   org.springframework.aop.framework.CglibAopProxy $ CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:738)     在   org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)     在   com.ryantenney.metrics.spring.TimedMethodInterceptor.invoke(TimedMethodInterceptor.java:48)     在   com.ryantenney.metrics.spring.TimedMethodInterceptor.invoke(TimedMethodInterceptor.java:34)     在   com.ryantenney.metrics.spring.AbstractMetricMethodInterceptor.invoke(AbstractMetricMethodInterceptor.java:59)     在   org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)     在   org.springframework.aop.framework.CglibAopProxy $ DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:673)     在   com.beequote.web.rest.QuotebuilderResource $$ EnhancerBySpringCGLIB $$ c16579cf.Quote()     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)at   sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)     在   sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)     在java.lang.reflect.Method.invoke(Method.java:498)at   org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:205)     在   org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:133)     在   org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:97)     在   org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:827)     在   org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:738)     在   org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)     在   org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:967)     在   org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:901)     在   org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)     在   org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:861)     在javax.servlet.http.HttpServlet.service(HttpServlet.java:687)at   org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)     在javax.servlet.http.HttpServlet.service(HttpServlet.java:790)at   io.undertow.servlet.handlers.ServletHandler.handleRequest(ServletHandler.java:85)     在   io.undertow.servlet.handlers.FilterHandler $ FilterChainImpl.doFilter(FilterHandler.java:129)     在   com.codahale.metrics.servlet.AbstractInstrumentedFilter.doFilter(AbstractInstrumentedFilter.java:111)     在   io.undertow.servlet.core.ManagedFilter.doFilter(ManagedFilter.java:61)     在   io.undertow.servlet.handlers.FilterHandler $ FilterChainImpl.doFilter(FilterHandler.java:131)     在   org.springframework.web.servlet.resource.ResourceUrlEncodingFilter.doFilter(ResourceUrlEncodingFilter.java:59)     在   io.undertow.servlet.core.ManagedFilter.doFilter(ManagedFilter.java:61)     在   io.undertow.servlet.handlers.FilterHandler $ FilterChainImpl.doFilter(FilterHandler.java:131)     在   org.springframework.boot.web.filter.ApplicationContextHeaderFilter.doFilterInternal(ApplicationContextHeaderFilter.java:55)     在   org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)     在   io.undertow.servlet.core.ManagedFilter.doFilter(ManagedFilter.java:61)     在   io.undertow.servlet.handlers.FilterHandler $ FilterChainImpl.doFilter(FilterHandler.java:131)     在   org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:101)     在   io.undertow.servlet.core.ManagedFilter.doFilter(ManagedFilter.java:61)     在   io.undertow.servlet.handlers.FilterHandler $ FilterChainImpl.doFilter(FilterHandler.java:131)     在   org.springframework.boot.actuate.trace.WebRequestTraceFilter.doFilterInternal(WebRequestTraceFilter.java:110)     在   org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)     在   io.undertow.servlet.core.ManagedFilter.doFilter(ManagedFilter.java:61)     在   io.undertow.servlet.handlers.FilterHandler $ FilterChainImpl.doFilter(FilterHandler.java:131)     在   org.springframework.security.web.FilterChainProxy $ VirtualFilterChain.doFilter(FilterChainProxy.java:317)     在   org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:127)     在   org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:91)     在   org.springframework.security.web.FilterChainProxy $ VirtualFilterChain.doFilter(FilterChainProxy.java:331)     在   org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:114)     在   org.springframework.security.web.FilterChainProxy $ VirtualFilterChain.doFilter(FilterChainProxy.java:331)     在   org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:137)     在   org.springframework.security.web.FilterChainProxy $ VirtualFilterChain.doFilter(FilterChainProxy.java:331)     在   org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:111)     在   org.springframework.security.web.FilterChainProxy $ VirtualFilterChain.doFilter(FilterChainProxy.java:331)     在   org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:170)     在   org.springframework.security.web.FilterChainProxy $ VirtualFilterChain.doFilter(FilterChainProxy.java:331)     在   org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.java:63)     在   org.springframework.security.web.FilterChainProxy $ VirtualFilterChain.doFilter(FilterChainProxy.java:331)     在com.beequote.security.jwt.JWTFilter.doFilter(JWTFilter.java:36)     在   org.springframework.security.web.FilterChainProxy $ VirtualFilterChain.doFilter(FilterChainProxy.java:331)     在   org.springframework.web.filter.CorsFilter.doFilterInternal(CorsFilter.java:96)     在   org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)     在   org.springframework.security.web.FilterChainProxy $ VirtualFilterChain.doFilter(FilterChainProxy.java:331)     在   org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:116)     在   org.springframework.security.web.FilterChainProxy $ VirtualFilterChain.doFilter(FilterChainProxy.java:331)     在   org.springframework.security.web.header.HeaderWriterFilter.doFilterInternal(HeaderWriterFilter.java:64)     在   org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)     在   org.springframework.security.web.FilterChainProxy $ VirtualFilterChain.doFilter(FilterChainProxy.java:331)     在   org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:105)     在   org.springframework.security.web.FilterChainProxy $ VirtualFilterChain.doFilter(FilterChainProxy.java:331)     在   org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter.doFilterInternal(WebAsyncManagerIntegrationFilter.java:56)     在   org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)     在   org.springframework.security.web.FilterChainProxy $ VirtualFilterChain.doFilter(FilterChainProxy.java:331)     在   org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:214)     在   org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:177)     在   org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:347)     在   org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:263)     在   io.undertow.servlet.core.ManagedFilter.doFilter(ManagedFilter.java:61)     在   io.undertow.servlet.handlers.FilterHandler $ FilterChainImpl.doFilter(FilterHandler.java:131)     在   org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:99)     在   org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)     在   io.undertow.servlet.core.ManagedFilter.doFilter(ManagedFilter.java:61)     在   io.undertow.servlet.handlers.FilterHandler $ FilterChainImpl.doFilter(FilterHandler.java:131)     在   org.springframework.web.filter.HttpPutFormContentFilter.doFilterInternal(HttpPutFormContentFilter.java:108)     在   org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)     在   io.undertow.servlet.core.ManagedFilter.doFilter(ManagedFilter.java:61)     在   io.undertow.servlet.handlers.FilterHandler $ FilterChainImpl.doFilter(FilterHandler.java:131)     在   org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:81)     在   org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)     在   io.undertow.servlet.core.ManagedFilter.doFilter(ManagedFilter.java:61)     在   io.undertow.servlet.handlers.FilterHandler $ FilterChainImpl.doFilter(FilterHandler.java:131)     在   org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:197)     在   org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)     在   io.undertow.servlet.core.ManagedFilter.doFilter(ManagedFilter.java:61)     在   io.undertow.servlet.handlers.FilterHandler $ FilterChainImpl.doFilter(FilterHandler.java:131)     在   io.undertow.servlet.handlers.FilterHandler.handleRequest(FilterHandler.java:84)     在   io.undertow.servlet.handlers.security.ServletSecurityRoleHandler.handleRequest(ServletSecurityRoleHandler.java:62)     在   io.undertow.servlet.handlers.ServletChain $ 1.handleRequest(ServletChain.java:64)     在   io.undertow.servlet.handlers.ServletDispatchingHandler.handleRequest(ServletDispatchingHandler.java:36)     在   io.undertow.servlet.handlers.security.SSLInformationAssociationHandler.handleRequest(SSLInformationAssociationHandler.java:131)     在   io.undertow.servlet.handlers.security.ServletAuthenticationCallHandler.handleRequest(ServletAuthenticationCallHandler.java:57)     在   io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)     在   io.undertow.security.handlers.AbstractConfidentialityHandler.handleRequest(AbstractConfidentialityHandler.java:46)     在   io.undertow.servlet.handlers.security.ServletConfidentialityConstraintHandler.handleRequest(ServletConfidentialityConstraintHandler.java:64)     在   io.undertow.security.handlers.AuthenticationMechanismsHandler.handleRequest(AuthenticationMechanismsHandler.java:60)     在   io.undertow.servlet.handlers.security.CachedAuthenticatedSessionHandler.handleRequest(CachedAuthenticatedSessionHandler.java:77)     在   io.undertow.security.handlers.AbstractSecurityContextAssociationHandler.handleRequest(AbstractSecurityContextAssociationHandler.java:43)     在   io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)     在   io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)     在   io.undertow.servlet.handlers.ServletInitialHandler.handleFirstRequest(ServletInitialHandler.java:292)     在   io.undertow.servlet.handlers.ServletInitialHandler.access $ 100(ServletInitialHandler.java:81)     在   io.undertow.servlet.handlers.ServletInitialHandler $ 2.call(ServletInitialHandler.java:138)     在   io.undertow.servlet.handlers.ServletInitialHandler $ 2.call(ServletInitialHandler.java:135)     在   io.undertow.servlet.core.ServletRequestContextThreadSetupAction $ 1.call(ServletRequestContextThreadSetupAction.java:48)     在   io.undertow.servlet.core.ContextClassLoaderSetupAction $ 1.call(ContextClassLoaderSetupAction.java:43)     在   io.undertow.servlet.handlers.ServletInitialHandler.dispatchRequest(ServletInitialHandler.java:272)     在   io.undertow.servlet.handlers.ServletInitialHandler.access $ 000(ServletInitialHandler.java:81)     在   io.undertow.servlet.handlers.ServletInitialHandler $ 1.handleRequest(ServletInitialHandler.java:104)     在   io.undertow.server.Connectors.executeRootHandler(Connectors.java:332)     在   io.undertow.server.HttpServerExchange $ 1.run(HttpServerExchange.java:830)     在   java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)     在   java.util.concurrent.ThreadPoolExecutor中的$ Worker.run(ThreadPoolExecutor.java:624)     在java.lang.Thread.run(Thread.java:748)

2 个答案:

答案 0 :(得分:0)

您应该使用

getResourceAsStream(...);

当资源捆绑为jar / war或任何其他单个文件包时。

看到的是,jar是一个单独的文件(有点像zip文件),它们将大量文件放在一起。从Os的pov,它是一个单独的文件,如果你想访问文件的一部分(你的图像文件),你必须将它用作流

答案 1 :(得分:0)

使用

创建OutputStream
OutputStream os = this.getClass()
    .getClassLoader()
    .getResourceAsStream("/templates/" + quoteNumber + ".pdf");

您希望将pdf存储在jar中,这与在使用OS文件系统的本地环境(例如使用Eclipse)中运行时不同。