Browse Source

Merge pull request #1842 from zjr2000/master

fix: issue 1840, inputStream auto close even autoClosedStream is false
pull/2074/head
Jiaju Zhuang 4 years ago committed by GitHub
parent
commit
5038f0606f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  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

@ -149,9 +149,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