FileSystemResource:java.io.FileNotFoundException

时间:2015-09-02 19:58:31

标签: 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网址创建FileSystemResource

1 个答案:

答案 0 :(得分:1)

您可以使用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