Java poi 3.6版本无法使用hsmf的解决办法

一、问题概述

要做一个 jar 包读取Outlook的.msg文件,用的是Apache POI包,3.6版本官方的例程如下:

poi 3.6:

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.Iterator;
import java.util.Map;

import org.apache.poi.hsmf.MAPIMessage;
import org.apache.poi.hsmf.datatypes.AttachmentChunks;
import org.apache.poi.hsmf.exceptions.ChunkNotFoundException;

// You need poi-scratchpad-3.6  and poi-3.6 ( http://poi.apache.org/ )

public class DetectMSGAttachment {
  public static void main (String ... args) throws IOException {
    String msgfile = "c:/temp/messagewithattachment.msg";
    MAPIMessage msg = new MAPIMessage(msgfile);
    Map attachmentMap = msg.getAttachmentFiles();
    if(attachmentMap.size() > 0) {
      for (Iterator ii = attachmentMap.entrySet().iterator(); ii.hasNext();) {
        Map.Entry entry = (Map.Entry)ii.next();
        String attachmentfilename = entry.getKey().toString();
        System.out.println(attachmentfilename);

        // extract attachment
        ByteArrayInputStream fileIn = (ByteArrayInputStream)entry.getValue();
        File f = new File("c:/temp", attachmentfilename); // output
        OutputStream fileOut = null;
        try {
          fileOut = new FileOutputStream(f);
          byte[] buffer = new byte[2048];
          int bNum = fileIn.read(buffer);
          while(bNum > 0) {
            fileOut.write(buffer);
            bNum = fileIn.read(buffer);
          }
        }
        finally {
          try {
            if(fileIn != null) {
              fileIn.close();
            }
          }
          finally {
            if(fileOut != null) {
              fileOut.close();
            }
          }
        }
      }
    }
    else {
      System.out.println("No attachment");
    }
  }
}

在Maven里导入POI 3.6,按照Maven官方的POI 3.6:

        
        
            org.apache.poi
            poi-scratchpad
            3.6
        

成功导入后,发现import org.apache.poi.hsmf.* 时,报错,显示没有该包。 


二、问题解决

仔细阅读Apache POI官方网页,发现如果要使用org.apache.poi.hsmf,需要更改artifactId为poi-scratchpad。详细代码如下:

        
        
            org.apache.poi
            poi-scratchpad
            3.6
        

这样,就可以使用hsmf包了

你可能感兴趣的:(Java,Web)