我正在尝试加载打包在war文件中的PNG文件,以便可以将其内联到电子邮件中,但是对我来说,我似乎无法正确地获取输入流。实例化时出现“ InputStream不能为空”错误。
项目结构:
src
|
---main
|
---java
| |
| ---com.company.team.app_name
| |
| ---service
| |
| mailservice.java
|
---resources
|
---content
|
---images
|
---logo.png
战争文件中的路径:
WEB-INF/classes/com/company/team/app_name/service/MailService.class
...
content/images/logo.png
mailservice代码(在类构造函数中):
this.logoBytes = new InputStreamResource(this.getClass()
.getClassLoader()
.getResourceAsStream("/content/images/logo.png"));
答案 0 :(得分:1)
使用ResourceUtils
中的org.springframework.util.ResourceUtils
;
try {
File file = ResourceUtils.getFile("classpath:content/images/logo.png")
byte[] logoBytes = Files.readAllBytes(file.toPath());
} catch (IOException e) {
e.printStackTrace();
}