Tôi muốn gửi email có tệp đính kèm tệp zip .. Tôi có thể gửi tệp pdf mà không lưu chúng vào vị trí thực tế bằng cách sử dụng ByteArrayOutputStream. Nhưng khi tôi thử zip những tập tin gửi nó không hoạt động. Nó cho phép đính kèm bất hợp pháp ngoại lệ.Cách gửi tệp zip mà không cần tạo tệp đó trên vị trí thực?
Dưới đây là mã tôi đã viết để tạo mã 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;
}
Bạn có thể thêm stacktrace không? –