Browse Source

修改读的关闭流无效 [Issue #1840]

developing
Jiaju Zhuang 4 years ago
parent
commit
46f5ffba9c
  1. 7
      src/main/java/com/alibaba/excel/analysis/v07/XlsxSaxAnalyser.java
  2. 44
      src/main/java/com/alibaba/excel/util/FileUtils.java
  3. 1
      update.md

7
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);
}

44
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);

1
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

Loading…
Cancel
Save