我想發送電子郵件與zip文件附件..我能夠發送pdf文件,而不使用ByteArrayOutputStream保存在物理位置。但是當我嘗試壓縮這些文件時,發送它不起作用。它給出例外非法附件。如何發送zip文件而無需在物理位置創建它?
下面是我寫的創建zip的代碼。
private MimeBodyPart zipAttachment(List<ByteArrayOutputStream> attachmentList, List<String> reportFileNames)
{
MimeBodyPart messageBodyPart = null;
try
{
// File file = File.createTempFile("Reports.zip",".tmp");
// FileOutputStream fout = new FileOutputStream(file);
ByteArrayOutputStream bout = new ByteArrayOutputStream(attachmentList.size());
ZipOutputStream zos = new ZipOutputStream(bout);
ZipEntry entry;
for(int i = 0; i < attachmentList.size(); i++)
{
ByteArrayOutputStream attachmentFile = attachmentList.get(i);
byte[] bytes = attachmentFile.toByteArray();
entry = new ZipEntry(reportFileNames.get(i));
entry.setSize(bytes.length);
zos.putNextEntry(entry);
zos.write(bytes);
}
messageBodyPart = new MimeBodyPart();
DataSource source = new ByteArrayDataSource(bout.toByteArray(), "application/zip");
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName("Reports.zip");
}
catch(Exception e)
{
// TODO: handle exception
}
return messageBodyPart;
}
你可以添加堆棧跟蹤嗎? – 2012-03-19 07:39:59