gpt4 book ai didi

来自 URL : "FileNotFoundException" 的带有 FileSystemResource 的 Java 邮件附件

转载 作者:行者123 更新时间:2023-12-02 09:31:54 24 4
gpt4 key购买 nike

我正在使用 Java Mail 发送带有附件的电子邮件。附件不是本地存储在应用程序下,而是从 URL 动态提取。

    List<FileSystemResource> fileSystemResources = new ArrayList<FileSystemResource>();
fileSystemResources.add(new FileSystemResource(
"http://myapp-dev.host.com/app/File1.PDF"));

//... Prepare email with dynamic-URL-obtained attachments...
MimeMessage msg = mailSender.createMimeMessage();
MimeMessageHelper message = new MimeMessageHelper(msg, true, "UTF-8");
for (FileSystemResource fileSystemResource : fileSystemResources) {
message.addAttachment(fileSystemResource.getFilename(), fileSystemResource);
}

//... at the end, send
mailSender.send(msg);

资源存在,我可以独立验证它:例子,http://myapp-dev.host.com/app/File1.PDF

但是我抛出以下异常:

java.lang.Exception]: org.springframework.mail.MailSendException: 
Failed messages: javax.mail.MessagingException: IOException while sending message;
nested exception is:
java.io.FileNotFoundException: http://myapp-dev.host.com/app/File1.PDF
(No such file or directory);

我见过的大多数示例都将 FileSystemResource 用于本地文件,而不是 URI 资源。代码有问题吗?

最佳答案

FileSystemResource 的 JavaDoc说:

Resource implementation for java.io.File and java.nio.file.Path handles with a file system target. Supports resolution as a File and also as a URL.

它仅处理 file:// URL,而不是 HTTP URL。

对于任何 URL(HTTP 和 file://),您可以使用 UrlResource :

Resource implementation for java.net.URL locators. Supports resolution as a URL and also as a File in case of the "file:" protocol.

关于来自 URL : "FileNotFoundException" 的带有 FileSystemResource 的 Java 邮件附件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57893666/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com