Example usage for org.apache.poi.hsmf.datatypes AttachmentChunks getAttachData

List of usage examples for org.apache.poi.hsmf.datatypes AttachmentChunks getAttachData

Introduction

In this page you can find the example usage for org.apache.poi.hsmf.datatypes AttachmentChunks getAttachData.

Prototype

public ByteChunk getAttachData() 

Source Link

Usage

From source file:org.silverpeas.core.mail.extractor.MSGExtractor.java

License:Open Source License

@Override
public List<MailAttachment> getAttachments() throws MailException {
    try {/*from  w w  w .j  a va 2s  .  com*/
        AttachmentChunks[] attachmentChunks = message.getAttachmentFiles();
        List<MailAttachment> mailAttachments = new ArrayList<>(attachmentChunks.length);
        for (AttachmentChunks attachment : attachmentChunks) {
            byte[] data = attachment.getAttachData().getValue();
            String fileName = attachment.getAttachLongFileName().getValue();
            MailAttachment mailAttachment = new MailAttachment(fileName);
            String dir = FileRepositoryManager.getTemporaryPath() + "mail"
                    + Calendar.getInstance().getTimeInMillis();
            File file = new File(dir, fileName);
            FileUtils.writeByteArrayToFile(file, data);
            mailAttachment.setPath(file.getAbsolutePath());
            mailAttachment.setSize(file.length());
            mailAttachments.add(mailAttachment);
        }
        return mailAttachments;
    } catch (IOException e) {
        throw new MailException(e);
    }
}