从Spring JAR读取文件

时间:2016-12-27 10:31:39

标签: java spring spring-boot handlebars.js

我正在开发一个Spring Boot应用程序,需要访问某些文件才能创建电子邮件模板。

部署@ localhost时,此代码有效:

ClassLoader classLoader = getClass().getClassLoader();
File bottom = new File(classLoader.getResource("email-templates/_bottom.hbs").getFile());
File top = new File(classLoader.getResource("email-templates/_top.hbs").getFile());
File contentFile = new File(classLoader.getResource("email-templates/" + template + ".hbs").getFile());

该应用程序正在部署为jar,在进入生产时会出现此错误:

2016-12-27 11:07:07.676  INFO 11334 --- [nio-8082-exec-8] c.s.xxxx.service.EmailService     : [ERROR] while sending email
java.io.FileNotFoundException: file:/home/webapp/xxxx/target/xxxx.jar!/email-templates/student-signup.hbs (No such file or directory)
    at java.io.FileInputStream.open0(Native Method)
    at java.io.FileInputStream.open(FileInputStream.java:195)
    at java.io.FileInputStream.<init>(FileInputStream.java:138)
    at com.github.jknack.handlebars.internal.Files.read(Files.java:64)
    at com.soamee.xxxx.service.impl.EmailServiceMailgunImpl.sendWithContent(EmailServiceMailgunImpl.java:191)
    at com.soamee.xxxx.service.impl.EmailServiceMailgunImpl.sendRegisteredStudent(EmailServiceMailgunImpl.java:130)
    at com.soamee.xxxx.service.impl.UserServiceImpl.createUser(UserServiceImpl.java:108)
    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.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:302)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:190)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
    at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:99)
    at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:281)
    at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:208)
    at com.sun.proxy.$Proxy143.createUser(Unknown Source)
    at com.soamee.xxxx.controller.UserController.createUserStudent(UserController.java:97)
    at com.soamee.xxxx.controller.UserController$$FastClassBySpringCGLIB$$1f89763c.invoke(<generated>)
    at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
    at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:651)
    at com.soamee.xxxx.controller.UserController$$EnhancerBySpringCGLIB$$25a9a85c.createUserStudent(<generated>)
    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:221)
    at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:136)
    at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:110)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:832)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:743)
    at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:961)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:895)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:967)
    at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:869)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:648)

任何人都知道如何在两种环境中正确读取文件?

1 个答案:

答案 0 :(得分:4)

(我假设您尝试阅读的文件在您的类路径中,因为您在Spring Jar中说过它)

要从类路径中读取文件,请使用以下命令:

import java.io.InputStream;
....
InputStream in = this.getClass().getClassLoader().getResourceAsStream("email-templates/_bottom.hbs");
// read 'in' as a regular input stream 

确保email-templates位于类路径的根目录,并且它包含名为_bottom.hbs的文件资源。

请注意,无论资源是在Jar还是在展开的目录结构中,这都可以使用。

相关问题