如何从pst文件获取帖子附件

时间:2014-08-21 09:38:40

标签: java apache-tika pst

我使用java libpst和tika从pst文件中提取元数据,我使用了这段代码:

    int numberOfAttachments = email.getNumberOfAttachments();
    for (int x = 0; x < numberOfAttachments; x++) {
    PSTAttachment attach = email.getAttachment(x);
   InputStream attachmentStream = attach.getFileInputStream();
   // both long and short filenames can be used for attachments
   String filename = attach.getLongFilename();
   if (filename.isEmpty()) {
        filename = attach.getFilename();
   }
   FileOutputStream out = new FileOutputStream(filename);
   // 8176 is the block size used internally and should give the best performance
   int bufferSize = 8176;
   byte[] buffer = new byte[bufferSize];
   int count = attachmentStream.read(buffer);
   while (count == bufferSize) {
        out.write(buffer);
        count = attachmentStream.read(buffer);
   }
   byte[] endBuffer = new byte[count];
   System.arraycopy(buffer, 0, endBuffer, 0, count);
   out.write(endBuffer);
   out.close();
   attachmentStream.close();
   }

我有这个错误:

  Caused by: java.io.FileNotFoundException: Invalid file path
  at java.io.FileOutputStream.<init>(Unknown Source)
  at java.io.FileOutputStream.<init>(Unknown Source)

1 个答案:

答案 0 :(得分:0)

我也有同样的问题。因为filename包含不需要的空格。所以我删除了它。

在您的代码中使用filename = filename.trim();删除空格。