Heroku类路径FileNotFound异常



上下文:我有一个Spring Boot应用程序,在创建用户帐户后,我想在其中向用户发送电子邮件。我在资源文件夹中有一个newUser.html文件;电子邮件";以及";密码";字段,以及我想发送的一些图像。我使用Jsoup:

@Override
public void sendUserCreatedMail(CreateUserRequest createUserRequest) throws MessagingException, IOException {
//Get JavaMailSender
JavaMailSenderImpl mailSenderImpl = mailMapper.mapMailSender();
//set the html elements with Jsoup
Document doc = buildHtmlDocument(Main.class.getClassLoader().getResource("html/newUser.html").getPath());
Element loginElement = doc.getElementById("login");
Element passwordElement = doc.getElementById("password");
loginElement.text("Login: " + createUserRequest.getEmail());
passwordElement.text("Password: " + createUserRequest.getPassword());
String html = doc.toString();
// Add inline images, key + value
Map<String, String> inlineImages = new HashMap<String, String>();
if(Main.class.getClassLoader().getResource("images/rea-logo-rond.png").getPath() != null){
//Get Email images Path
String logoPath = Main.class.getClassLoader().getResource("images/rea-logo-rond.png").getPath();
inlineImages.put("logo", logoPath);
}
//Create email instance  & send mail
MimeMessage mimeMessage = mailMapper.mapCreateUserRequestToSender(createUserRequest, inlineImages, html);
mailSenderImpl.send(mimeMessage);
}

当在我的IDE中运行程序时,这一切都很好。然而,当我在Heroku上运行时,我会得到一个";FileNotFound";异常:

[java.io.FileNotFoundException: file:/app/rea-backend/target/rea-backend-0.0.1-SNAPSHOT.jar!/BOOT-INF/classes!/html/newUser.html (No such file or directory)]

我偶然发现了这篇SO文章,但它没有适合我的案例的解决方案。我希望我可以把newUser.html保存为DB中的一个Blob,并每次都得到它(尽管这不是一个很好的解决方案…);但是;文件";构造函数是有效的,它需要您尝试使用的文件的PATH字符串,而不是实际的文件本身。。。所需的图像也是如此。

必须有另一种方法来使用Heroku访问资源文件夹中的文件?还是我做错了什么?我不明白为什么它适用于本地,但不适用于Heroku。

在这一点上,选择另一个平台而不是Heroku不是一个选择。

正如M.Deinum所指出的,没有办法做到这一点,因为当这些文件被打包为jar时,这些图像不再是物理文件,只是jar中的资源。

我按照M.Deinum的建议,使用messageHelper.addInline()(用于图像)和InputStream(用于html文件),得到了一个解决方案。东西现在工作得很好!

@Override
public void sendUserCreatedMail(CreateUserRequest createUserRequest) throws MessagingException, IOException {
//Get JavaMailSender
JavaMailSenderImpl mailSenderImpl = mailMapper.mapMailSender();
//Create email instance  & send mail
MimeMessage mimeMessage = mailMapper.mapCreateUserRequestToMimeMessage(createUserRequest, mailSenderImpl.createMimeMessage());
mailSenderImpl.send(mimeMessage);
}
public MimeMessage mapCreateUserRequestToMimeMessage(CreateUserRequest createUserRequest, MimeMessage mimeMessage) throws MessagingException, IOException {
//set meta data using "MimeMessageHelper"
MimeMessageHelper helper = getMessageHelper(mimeMessage, createUserRequest.getEmail(),"Uw Real Estate Academy Account", "Real Estate Academy <" + this.emailConfiguration.getUsernameInfo()+">");
String htmlMessage = buildCreateUserRequestHtmlDocument("html/newUser.html", createUserRequest);
InputStream inputStream = Main.class.getClassLoader().getResourceAsStream("images/rea-logo-rond.png");
helper.setText(htmlMessage, true);
helper.addInline("logo", new ByteArrayResource(IOUtils.toByteArray(inputStream)), "image/png");
return mimeMessage;
}
public String buildCreateUserRequestHtmlDocument(String filePath, CreateUserRequest createUserRequest) throws IOException {
InputStream inputStream = Main.class.getClassLoader().getResourceAsStream(filePath);
Document doc = Jsoup.parse(inputStream, "UTF-8", "https://www.realestateacademy.be");
Element loginElement = doc.getElementById("login");
Element passwordElement = doc.getElementById("password");
loginElement.text("Login: " + createUserRequest.getEmail());
passwordElement.text("Password: " + createUserRequest.getPassword());
return doc.toString();
}

最新更新