我有一些代码
String url = http://site/some-file.pdf
HttpURLConnection fileRequest = (HttpURLConnection) new URL(fileUrl).openConnection();
Path file = Files.createFile(Paths.get("mydirectory" + "filename" +".pdf"));
Files.write(file, IOUtils.toByteArray(fileRequest.getInputStream()));
此代码写入从url获取文件并将其写入我的本地主页(例如,磁盘D)
如何将其附加到邮件发件人而不保存本地?有可能吗?
答案 0 :(得分:4)
您可以使用javax.activation.URLDataSource:
Part attachment = new MimeBodyPart();
URL url = new URL("http://site/some-file.pdf");
URLDataSource uds = new URLDataSource(url);
attachment.setDataHandler(new DataHandler(uds));
attachment.setDisposition(Part.ATTACHMENT);
attachment.setFileName(url.getFile());
根据文档,您只需使用DataHandler
和URL
Part attachment = new MimeBodyPart();
URL url = new URL("http://site/some-file.pdf");
attachment.setDataHandler(new DataHandler(url));
attachment.setDisposition(Part.ATTACHMENT);
attachment.setFileName(url.getFile());