java - FileSystemResource : java. io.FileNotFoundException

标签 java spring url filenotfoundexception

我尝试从 URL(我的 DropBox 帐户)创建如下所示的 fil,然后将其作为附件添加到 MimeMessageHelper,但我收到 FileNotFoundException。我可能做错了什么?

String[] attachments = {"https://dl.dropboxusercontent.com/s/XXXX/my%20Letter.docx"};

for (String attachment : attachments) {
    FileSystemResource file = new FileSystemResource("url:" + attachment);
    message.addAttachment(file.getFilename(), file);
}

错误:

Caused by: org.springframework.mail.MailSendException: Failed messages: javax.mail.MessagingException: IOException while sending message;
  nested exception is:
    java.io.FileNotFoundException: url:https:\dl.dropboxusercontent.com\s\XXXX\my%20Letter.docx (The filename, directory name, or volume label syntax is incorrect); message exceptions (1) are:
Failed message 1: javax.mail.MessagingException: IOException while sending message;
  nested exception is:
    java.io.FileNotFoundException: url:https:\dl.dropboxusercontent.com\s\XXXX\my%20Letter.docx (The filename, directory name, or volume label syntax is incorrect)
<小时/>

更新:

如何从 Http URL 创建 FileSystemResource

最佳答案

您可以使用 UrlResource 代替文件系统资源,或者使用某些 http 客户端下载文件,然后将其附加到电子邮件中。

例如:

URL website = new URL("http://someurl");
ReadableByteChannel rbc = Channels.newChannel(website.openStream());
FileOutputStream fos = new FileOutputStream("some temporary name");
fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);

然后使用FileSystemResource附加它

您可以使用 http 客户端来实现此目的,例如:okhttp

关于java - FileSystemResource : java. io.FileNotFoundException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32362022/

相关文章:

java.util.Logger 创建的文件比应有的多

java - 使用Selenium Web驱动程序访问Java GUI组件

python - PyQt:如何从菜单项加载 url

javascript - 根据 url 查询字符串加载自定义 css 并维护该查询字符串上的用户

javascript - 拖放 : How to get the URL of image being dropped if image is a link (not the url of the link)

java - Android 工作室背景图片

java - jsp/servlet 中的 Web 应用程序 - 使用 fmt 进行本地化 :message

spring - 运行两个Spring Boot实例

java - spring 指标中的@Timed 注释

java - Spring Boot JPA 使用查询填充实体字段