gpt4 book ai didi

java - 从 Spring JAR 读取文件

转载 作者:行者123 更新时间:2023-12-01 18:00:32 26 4
gpt4 key购买 nike

我正在开发一个 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)

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

最佳答案

(我假设您尝试读取的文件位于您的类路径中,因为您说它位于 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 还是分解的目录结构中,这都将起作用。

关于java - 从 Spring JAR 读取文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41343427/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com