Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/349.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在jar文件JavaSpring中加载资源_Java_Spring_File_Jar - Fatal编程技术网

在jar文件JavaSpring中加载资源

在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

我想加载位于以下位置的html文件:

在独立的
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”)
,这会很快失败)?