简体   繁体   中英

FileSystemResource: java.io.FileNotFoundException

I'm trying to create a fil as follows from a URL (my DropBox account), then add it as an attachment to a MimeMessageHelper, but I get a FileNotFoundException . What could I be doing wrong?

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);
}

Error:

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)

UPDATE:

How can I go about creating a FileSystemResource from a Http URL?

You can use UrlResource instead of File system resource or download the file using some http client and then attach it to email.

For example:

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);

And then attach it using FileSystemResource

You can use http client for this, for example: okhttp

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM