使用outputstream上传时文件已损坏

时间:2018-05-30 06:40:18

标签: java excel file-upload outputstream aspose

每当我上传带有部分数据的Excel时,上传的文件都会损坏,我无法在aspose工作簿中使用此上传的文件(在某些情况下,它也有效,我不知道为什么)。即便如此,我打开它,excel显示损坏的文件消息。这里是片段

InputStream stream = file.getInputStream();


OutputStream os = new FileOutputStream("path_to_xlsFile.xls");
BufferedOutputStream bos = new BufferedOutputStream(os);
int bytesRead;
byte[] buffer = new byte[8192];

while ((bytesRead = stream.read(buffer, 0, 8192)) != -1) {
       bos.write(buffer, 0, bytesRead);
 }
bos.flush();
bos.close();
os.close();
stream.close();

我通过aspose库打开工作簿时遇到的错误,对于相同的上传文件是

java.lang.IllegalStateException: The FAT in the structured storage document seems to be corrupted.
at com.aspose.cells.py.b(Unknown Source)
at com.aspose.cells.py.b(Unknown Source)
at com.aspose.cells.py.<init>(Unknown Source)
at com.aspose.cells.Workbook.a(Unknown Source)
at com.aspose.cells.Workbook.open(Unknown Source)

java.lang.IllegalStateException: The structured storage seems to be corrupt.
at com.aspose.cells.mm.<init>(Unknown Source)
at com.aspose.cells.py.c(Unknown Source)
at com.aspose.cells.py.b(Unknown Source)
at com.aspose.cells.py.<init>(Unknown Source)
at com.aspose.cells.Workbook.a(Unknown Source)
at com.aspose.cells.Workbook.open(Unknown Source)

java.lang.IllegalArgumentException: length
at com.aspose.cells.b.a.d.h.b(Unknown Source)
at com.aspose.cells.py.c(Unknown Source)
at com.aspose.cells.py.b(Unknown Source)
at com.aspose.cells.py.<init>(Unknown Source)
at com.aspose.cells.Workbook.a(Unknown Source)
at com.aspose.cells.Workbook.open(Unknown Source)

修改

如果我使用file.transferTo(outputFile),那么对于少数文件我也会遇到此异常

java.lang.IllegalStateException: The FAT in the structured storage document seems to be corrupted.
at com.aspose.cells.py.b(Unknown Source)
at com.aspose.cells.py.b(Unknown Source)
at com.aspose.cells.py.<init>(Unknown Source)
at com.aspose.cells.Workbook.a(Unknown Source)
at com.aspose.cells.Workbook.open(Unknown Source)

1 个答案:

答案 0 :(得分:0)

好吧,您似乎从模板XLS文件中获取了部分数据,这可能会否定/恶化整个Excel文件格式规范或标准。因此,以字节数组(或流)提取的文件不会遵循MS Excel文件格式结构或规则。简而言之,文件将被破坏,MS Excel也会在您提到的文件打开时显示错误消息。这可能不是Aspose.Cells API的问题。如果工作正常,您可以尝试以下示例代码段(两者中的任何一个): 例如 示例代码:

1)
Path path = Paths.get("path_to_xlsFile.xls"); 
ByteArrayInputStream stream = new ByteArrayInputStream(Files.readAllBytes(path)); 

Workbook workbook = new Workbook(stream); 
.........

2)
String testFileTemplate = "path_to_xlsFile.xls"; 
FileInputStream fis = new FileInputStream(new File(testFileTemplate)); 
Workbook workbook = new Workbook(fis); 

我是Aspose的支持开发人员/传播者。