我在电子邮件中收到一个文件,我使用 javamail API 阅读该文件并将其保存到磁盘目录中
邮件提取器.java
Multipart multiPart = (Multipart) message.getContent();
int numberOfParts = multiPart.getCount();
for (int partCount = 0; partCount < numberOfParts; partCount++) {
MimeBodyPart part = (MimeBodyPart) multiPart.getBodyPart(partCount);
if (Part.ATTACHMENT.equalsIgnoreCase(part.getDisposition())) {
// this part is attachment
String fileName = part.getFileName();
attachFiles += fileName + ", ";
part.saveFile(saveDir + File.separator + fileName);
}
else {
// this part may be the message content
messageContent = part.getContent().toString();
}
}
现在另一个程序轮询该磁盘目录并读取所有传入文件。我只关心文件扩展名 - xls/xlsx,所以我使用 apache poi 来读取文件:
/AvailabilityDirectoryPoller.java
FileInputStream fs = new FileInputStream(file);
Workbook wb = null;
Sheet sheet = null;
if(FilenameUtils.getExtension(file.getName()).equalsIgnoreCase("xls")){
wb = new HSSFWorkbook(fs); //line no 178
sheet = (HSSFSheet)wb.getSheetAt(0);
}
else{
wb = new XSSFWorkbook(fs);
sheet = (XSSFSheet)wb.getSheetAt(0);
}
这有时(随机读取)会导致如下错误
[2013-07-18 23:38:32,409] ERROR [AvailabilityDirectoryPoller]: Exception in I/O Reading file
java.io.IOException: Your file contains 79 sectors, but the initial DIFAT array at index 1 referenced block # 98. This isn't allowed and your file is corrupt
at org.apache.poi.poifs.storage.BlockAllocationTableReader.<init>(BlockAllocationTableReader.java:103)
at org.apache.poi.poifs.filesystem.POIFSFileSystem.<init>(POIFSFileSystem.java:151)
at org.apache.poi.hssf.usermodel.HSSFWorkbook.<init>(HSSFWorkbook.java:322)
at org.apache.poi.hssf.usermodel.HSSFWorkbook.<init>(HSSFWorkbook.java:303)
at com.db.mmrepo.app.avl.bespoke.AvailabilityDirectoryPoller.process(AvailabilityDirectoryPoller.java:178)
at com.db.mmrepo.app.avl.bespoke.AvailabilityDirectoryPoller.fileFound(AvailabilityDirectoryPoller.java:73)
at org.sadun.util.polling.DefaultListener.receive(DefaultListener.java:43)
at com.deltax.util.listener.SignalQueue.run(Unknown Source)
如果我在 MS Excel 中打开保存的文件,它会打开没有警告/错误。