在jar文件JavaSpring中加载资源
我想加载位于以下位置的html文件: 在独立的在jar文件JavaSpring中加载资源,java,spring,file,jar,Java,Spring,File,Jar,我想加载位于以下位置的html文件: 在独立的spring启动jar应用程序中使用 这种方法会导致FileSystemNotFoundException new InputStreamReader( getClass().getResourceAsStream("/email-templates/html-email.html") ) 使用 @Autowired private ResourceLoader resourceLoader; ... resourceLoad
spring启动jar应用程序中使用
这种方法会导致FileSystemNotFoundException
new InputStreamReader(
getClass().getResourceAsStream("/email-templates/html-email.html")
)
使用
@Autowired
private ResourceLoader resourceLoader;
...
resourceLoader.getResource("classpath:email-templates/html-email.html");
导致NullPointerException
请指定如何在SpringBootJAR中正确加载文件
在jar文件JavaSpring中加载资源
试试下面的代码
ClassLoader classloader = Thread.currentThread().getContextClassLoader();
InputStream is = classloader.getResourceAsStream("email-templates/html-email.html");
/email templates/html email.html
如果上述内容不起作用,请访问此帮助完整链接
在jar文件JavaSpring中加载资源
试试下面的代码
ClassLoader classloader = Thread.currentThread().getContextClassLoader();
InputStream is = classloader.getResourceAsStream("email-templates/html-email.html");
/email templates/html email.html
如果上述内容不起作用,请访问此帮助完整链接
请尝试下面的代码。使用SpringClassPathResource,您应该能够加载它们。只要您尝试在spring上下文中加载这个文件,spring容器就会知道它的类路径。所以它应该加载相应的文件。否则,它会尝试在文件系统中查找
Resource resource = new ClassPathResource("email-templates/html-email.html");
我假设您的jar中有这些资源,检查jar文件中是否有这些资源
jar tf springboot.jar
试试下面的代码。使用SpringClassPathResource,您应该能够加载它们。只要您尝试在spring上下文中加载这个文件,spring容器就会知道它的类路径。所以它应该加载相应的文件。否则,它会尝试在文件系统中查找
Resource resource = new ClassPathResource("email-templates/html-email.html");
我假设您的jar中有这些资源,检查jar文件中是否有这些资源
jar tf springboot.jar
这一个还会导致FileSystemNotFoundException
。是的,但也与文件系统
相关,您也可以通过签出更好地理解。这一个还会导致FileSystemNotFoundException
。是的,但也与文件系统
相关,您还可以签出以便更好地理解。为什么要尝试手动加载它,而不是使用TemplateResolver
或注入它(使用@Resource(“classpath:email templates/html email.html”)
,这会很快失败)?为什么您试图手动加载它,而不是使用TemplateResolver
或注入它(使用@Resource(“classpath:email templates/html email.html”)
,这会很快失败)?