2016-12-27 214 views
0

我正在開發需要訪問某些文件以創建電子郵件模板的Spring Boot應用程序。從Spring JAR讀取文件

在部署@本地,此代碼的工作:

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()); 

的應用程序被部署爲一個罐子,要生產時,它提供此錯誤:

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) 

任何人知道如何能我在兩種環境中正確讀取文件?

+0

不要像這樣加載它們。使用Spring'ResourceLoader'並使用spring資源抽象。 –

回答

4

(我假設你正在試圖讀取該文件是在類路徑中,因爲你說這是在春節罐)

若要從類路徑讀取文件,使用以下命令:

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中還是在分解目錄結構中,這都可以工作。