Browse Source

Merge remote-tracking branch 'origin/3.x' into 3.x

developing
Jiaju Zhuang 4 years ago
parent
commit
58503d8f27
  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

@ -164,9 +164,11 @@ public class XlsxSaxAnalyser implements ExcelReadExecutor {
xlsxReadWorkbookHolder.setTempFile(readTempFile);
File tempFile = new File(readTempFile.getPath(), UUID.randomUUID().toString() + ".xlsx");
if (decryptedStream != null) {
FileUtils.writeToFile(tempFile, decryptedStream);
if(xlsxReadWorkbookHolder.getAutoCloseStream()) FileUtils.writeToFile(tempFile, decryptedStream);
else FileUtils.writeToFileNotCloseStream(tempFile, decryptedStream);
} 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);
}

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() {
File poiFilesPathFile = new File(poiFilesPath);
createDirectory(poiFilesPathFile);

Loading…
Cancel
Save