2012-08-16 77 views
0

从来就得到了一个PDF文件作为ByteArray的,我想知道,如果there's的方式将其固定而不会在服务器上创建主文件。Playframework,二进制PDF文件附件问题

通过播放文件所提供的代码只允许真正的文件进行安装。

EmailAttachment attachment = new EmailAttachment(); 
attachment.setDescription("A pdf document"); 
attachment.setPath(Play.getFile("rules.pdf").getPath()); 

I'm使用Playframework邮件模块。

谢谢!

回答

2

由于播放1.x中使用Apache Commons Email库的引擎盖下,你可以使用MultiPartEmail#attach(DataSource ds, String name, String description)方法:

import org.apache.commons.mail.*; 

// create the mail 
MultiPartEmail email = new MultiPartEmail(); 
email.setHostName("mail.myserver.com"); 
email.addTo("[email protected]", "John Doe"); 
email.setFrom("[email protected]", "Me"); 
email.setSubject("The picture"); 
email.setMsg("Here is the picture you wanted"); 

// get your inputstream from your db 
InputStream is = new BufferedInputStream(MyUtils.getBlob()); 
DataSource source = new ByteArrayDataSource(is, "application/pdf"); 

// add the attachment 
email.attach(source, "somefile.pdf", "Description of some file"); 

// send the email 
email.send(); 
+0

这适用于模板? – axierjhtjz 2012-08-16 12:35:11

+0

按照你说的方式解决了它。非常感谢@nico_ekito – axierjhtjz 2012-08-17 11:50:45