Java 如何从Pst文件中获取Pst附件
我使用java libpst和tika从pst文件中提取元数据,我使用以下代码:Java 如何从Pst文件中获取Pst附件,java,apache-tika,pst,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.
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)
我也有同样的问题。因为文件名包含不需要的空格。所以我把它拿走了
在代码中使用filename=filename.trim;删除空格。是否检查了文件名?并确保其中没有任何操作系统不喜欢的字符?很好,没错,我只是添加了filename=filename.trim;很好用,谢谢