Browse Source

fix: issue 1804, inputStream auto close even autoClosedStream is false

pull/1842/head
ZJR 4 years ago
parent
commit
3e95d1d163
  1. 6
      src/main/java/com/alibaba/excel/analysis/v07/XlsxSaxAnalyser.java
  2. 22
      src/main/java/com/alibaba/excel/util/FileUtils.java

6
src/main/java/com/alibaba/excel/analysis/v07/XlsxSaxAnalyser.java

@ -147,9 +147,11 @@ public class XlsxSaxAnalyser implements ExcelReadExecutor {
xlsxReadWorkbookHolder.setTempFile(readTempFile); xlsxReadWorkbookHolder.setTempFile(readTempFile);
File tempFile = new File(readTempFile.getPath(), UUID.randomUUID().toString() + ".xlsx"); File tempFile = new File(readTempFile.getPath(), UUID.randomUUID().toString() + ".xlsx");
if (decryptedStream != null) { if (decryptedStream != null) {
FileUtils.writeToFile(tempFile, decryptedStream); if(xlsxReadWorkbookHolder.getAutoCloseStream()) FileUtils.writeToFile(tempFile, decryptedStream);
else FileUtils.writeToFileNotCloseStream(tempFile, decryptedStream);
} else { } else {
FileUtils.writeToFile(tempFile, xlsxReadWorkbookHolder.getInputStream()); if(xlsxReadWorkbookHolder.getAutoCloseStream()) FileUtils.writeToFile(tempFile, xlsxReadWorkbookHolder.getInputStream());
else FileUtils.writeToFileNotCloseStream(tempFile, xlsxReadWorkbookHolder.getInputStream());
} }
return OPCPackage.open(tempFile, PackageAccess.READ); return OPCPackage.open(tempFile, PackageAccess.READ);
} }

22
src/main/java/com/alibaba/excel/util/FileUtils.java

@ -132,6 +132,28 @@ public class FileUtils {
} }
} }
public static void writeToFileNotCloseStream(File file, InputStream inputStream) {
OutputStream outputStream = null;
try {
outputStream = new FileOutputStream(file);
int bytesRead;
byte[] buffer = new byte[WRITE_BUFF_SIZE];
while ((bytesRead = inputStream.read(buffer, 0, WRITE_BUFF_SIZE)) != -1) {
outputStream.write(buffer, 0, bytesRead);
}
} catch (Exception e) {
throw new ExcelAnalysisException("Can not create temporary file!", e);
} finally {
if (outputStream != null) {
try {
outputStream.close();
} catch (IOException e) {
throw new ExcelAnalysisException("Can not close 'outputStream'!", e);
}
}
}
}
public static void createPoiFilesDirectory() { public static void createPoiFilesDirectory() {
File poiFilesPathFile = new File(poiFilesPath); File poiFilesPathFile = new File(poiFilesPath);
createDirectory(poiFilesPathFile); createDirectory(poiFilesPathFile);

Loading…
Cancel
Save