From 46f5ffba9c08a4de4741ed2990ae977f588d304c Mon Sep 17 00:00:00 2001 From: Jiaju Zhuang Date: Mon, 12 Apr 2021 15:42:11 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E8=AF=BB=E7=9A=84=E5=85=B3?= =?UTF-8?q?=E9=97=AD=E6=B5=81=E6=97=A0=E6=95=88=20[Issue=20#1840]?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../excel/analysis/v07/XlsxSaxAnalyser.java | 7 ++- .../com/alibaba/excel/util/FileUtils.java | 44 +++++++------------ update.md | 1 + 3 files changed, 21 insertions(+), 31 deletions(-) diff --git a/src/main/java/com/alibaba/excel/analysis/v07/XlsxSaxAnalyser.java b/src/main/java/com/alibaba/excel/analysis/v07/XlsxSaxAnalyser.java index f833e79b..1b6e6a8b 100644 --- a/src/main/java/com/alibaba/excel/analysis/v07/XlsxSaxAnalyser.java +++ b/src/main/java/com/alibaba/excel/analysis/v07/XlsxSaxAnalyser.java @@ -164,11 +164,10 @@ public class XlsxSaxAnalyser implements ExcelReadExecutor { xlsxReadWorkbookHolder.setTempFile(readTempFile); File tempFile = new File(readTempFile.getPath(), UUID.randomUUID().toString() + ".xlsx"); if (decryptedStream != null) { - if(xlsxReadWorkbookHolder.getAutoCloseStream()) FileUtils.writeToFile(tempFile, decryptedStream); - else FileUtils.writeToFileNotCloseStream(tempFile, decryptedStream); + FileUtils.writeToFile(tempFile, decryptedStream, false); } else { - if(xlsxReadWorkbookHolder.getAutoCloseStream()) FileUtils.writeToFile(tempFile, xlsxReadWorkbookHolder.getInputStream()); - else FileUtils.writeToFileNotCloseStream(tempFile, xlsxReadWorkbookHolder.getInputStream()); + FileUtils.writeToFile(tempFile, xlsxReadWorkbookHolder.getInputStream(), + xlsxReadWorkbookHolder.getAutoCloseStream()); } return OPCPackage.open(tempFile, PackageAccess.READ); } diff --git a/src/main/java/com/alibaba/excel/util/FileUtils.java b/src/main/java/com/alibaba/excel/util/FileUtils.java index 09192473..bc6b5d8a 100644 --- a/src/main/java/com/alibaba/excel/util/FileUtils.java +++ b/src/main/java/com/alibaba/excel/util/FileUtils.java @@ -9,12 +9,12 @@ import java.io.InputStream; import java.io.OutputStream; import java.util.UUID; -import org.apache.poi.util.DefaultTempFileCreationStrategy; -import org.apache.poi.util.TempFile; - import com.alibaba.excel.exception.ExcelAnalysisException; import com.alibaba.excel.exception.ExcelCommonException; +import org.apache.poi.util.DefaultTempFileCreationStrategy; +import org.apache.poi.util.TempFile; + /** * * @author jipengfei @@ -100,10 +100,21 @@ public class FileUtils { /** * Write inputStream to file * - * @param file - * @param inputStream + * @param file file + * @param inputStream inputStream */ public static void writeToFile(File file, InputStream inputStream) { + writeToFile(file, inputStream, true); + } + + /** + * Write inputStream to file + * + * @param file file + * @param inputStream inputStream + * @param closeInputStream closeInputStream + */ + public static void writeToFile(File file, InputStream inputStream, boolean closeInputStream) { OutputStream outputStream = null; try { outputStream = new FileOutputStream(file); @@ -122,7 +133,7 @@ public class FileUtils { throw new ExcelAnalysisException("Can not close 'outputStream'!", e); } } - if (inputStream != null) { + if (inputStream != null && closeInputStream) { try { inputStream.close(); } catch (IOException e) { @@ -132,27 +143,6 @@ 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() { File poiFilesPathFile = new File(poiFilesPath); diff --git a/update.md b/update.md index 4e07a60a..119758fb 100644 --- a/update.md +++ b/update.md @@ -11,6 +11,7 @@ * 修复没有样式的情况下空指针异常 [Issue #1738](https://github.com/alibaba/easyexcel/issues/1738) * 修改异常抛出逻辑 [Issue #1618](https://github.com/alibaba/easyexcel/issues/1618) * 兼容一些非官方excel的情况 [Issue #1527](https://github.com/alibaba/easyexcel/issues/1527) +* 修改读的关闭流无效 [Issue #1840](https://github.com/alibaba/easyexcel/issues/1840) # 2.2.8