从Docker容器中的Springboot Resources读取文件

发布于 2025-02-11 08:30:17 字数 1758 浏览 1 评论 0原文

我正在尝试在电子邮件正文中发送带有内联图像的电子邮件。当我从Eclipse本地运行它时,该代码正常工作。但是,当它被停靠并部署到kubernetes群集时,它无法读取PNG文件。

我会遇到错误“ java.io.filenotfoundexception:类路径资源[external_files/email_template.png]无法解决到绝对文件路径,因为它不属于文件系统:jar:jar:file:/app.jar!/boot!/boot! -inf/class!/external_files/email_template.png“ 项目结构屏幕截图

MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(fromEmail));
message.setSubject(AppConstants.EMAIL_SUBJECT);
MimeMultipart multipart = new MimeMultipart(AppConstants.RELATED);
MimeBodyPart messageBodyPart = new MimeBodyPart();
String html = AppConstants.EMAIL_HTML_PART_1 + recepientName + 
AppConstants.EMAIL_HTML_PART_2
    + fromName + AppConstants.EMAIL_HTML_PART_3 + appProperties.getEmailOnboardPage()
    + AppConstants.EMAIL_HTML_PART_4 + appProperties.getEmailHelpPage()
    + AppConstants.EMAIL_HTML_PART_5;
messageBodyPart.setContent(html, AppConstants.TEXT_HTML);
multipart.addBodyPart(messageBodyPart);

MimeBodyPart messageBodyPart2 = new MimeBodyPart();
DataSource fds = new FileDataSource(ResourceUtils.getFile(src/main/resources/External_Files/email_template.png));
 messageBodyPart2.setDataHandler(new DataHandler(fds));
 messageBodyPart2.setHeader(AppConstants.CONTENT_ID, AppConstants.IMAGE_HEADER);
 multipart.addBodyPart(messageBodyPart2);

 message.setContent(multipart);
message.setHeader(AppConstants.X_PRIORITY, AppConstants.ONE);
message.addRecipient(Message.RecipientType.TO, new InternetAddress(recipient));
log.info("executing Transport.send");
Transport.send(message);

png

I am trying to send email with inline image in the email body. The code is working fine when I am running it locally from eclipse. But when it's getting dockerized and deployed to Kubernetes cluster it's unable to read the png file.

I am getting the error "java.io.FileNotFoundException: class path resource [External_Files/email_template.png] cannot be resolved to absolute file path because it does not reside in the file system: jar:file:/app.jar!/BOOT-INF/classes!/External_Files/email_template.png"
Project structure screen shot

MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(fromEmail));
message.setSubject(AppConstants.EMAIL_SUBJECT);
MimeMultipart multipart = new MimeMultipart(AppConstants.RELATED);
MimeBodyPart messageBodyPart = new MimeBodyPart();
String html = AppConstants.EMAIL_HTML_PART_1 + recepientName + 
AppConstants.EMAIL_HTML_PART_2
    + fromName + AppConstants.EMAIL_HTML_PART_3 + appProperties.getEmailOnboardPage()
    + AppConstants.EMAIL_HTML_PART_4 + appProperties.getEmailHelpPage()
    + AppConstants.EMAIL_HTML_PART_5;
messageBodyPart.setContent(html, AppConstants.TEXT_HTML);
multipart.addBodyPart(messageBodyPart);

MimeBodyPart messageBodyPart2 = new MimeBodyPart();
DataSource fds = new FileDataSource(ResourceUtils.getFile(src/main/resources/External_Files/email_template.png));
 messageBodyPart2.setDataHandler(new DataHandler(fds));
 messageBodyPart2.setHeader(AppConstants.CONTENT_ID, AppConstants.IMAGE_HEADER);
 multipart.addBodyPart(messageBodyPart2);

 message.setContent(multipart);
message.setHeader(AppConstants.X_PRIORITY, AppConstants.ONE);
message.addRecipient(Message.RecipientType.TO, new InternetAddress(recipient));
log.info("executing Transport.send");
Transport.send(message);

png

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

赠意 2025-02-18 08:30:17

@Indranil Halder我知道回答已经晚了,但是对于有这个问题的其他人来说,发现此问题很有用,

您可以使用Resoursceloader在Spring Jar文件中找到classpath

@Autowired
ResourceLoader resourceLoader;
Resource resource = resourceLoader.getResource("classpath:External_Files/email_template.png");

@Indranil Halder I know it's late to answer but for others who have this problem finding this issue would be useful

you can use ResourceLoader to find classpath in spring jar file

@Autowired
ResourceLoader resourceLoader;
Resource resource = resourceLoader.getResource("classpath:External_Files/email_template.png");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文