2016-12-27 8 views
0

電子メールテンプレートを作成するためにいくつかのファイルにアクセスする必要があるSpring Bootアプリケーションを開発しています。Spring JARからファイルを読む

は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) 

誰でもできる方法を知っています私は両方の環境でファイルを正しく読みましたか?

+0

このようにロードしないでください。 Springの 'ResourceLoader'を使用し、springsリソースの抽象化を使用してください。 –

答えて

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ディレクトリ構造であるか、展開されたディレクトリ構造であるかに関係なく機能することに注意してください。

関連する問題