Browse Source

Merge pull request #1976 from Hccake/bugfix-EncryptedExcelCorrupted

🐛 fix 当进行加密导出时,由于输出流未关闭导致的文件损坏异常
developing
Jiaju Zhuang 3 years ago committed by GitHub
parent
commit
aad90c6e8b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 13
      src/main/java/com/alibaba/excel/context/WriteContextImpl.java

13
src/main/java/com/alibaba/excel/context/WriteContextImpl.java

@ -483,6 +483,19 @@ public class WriteContextImpl implements WriteContext {
POIFSFileSystem fileSystem = new POIFSFileSystem(); POIFSFileSystem fileSystem = new POIFSFileSystem();
Encryptor encryptor = new EncryptionInfo(EncryptionMode.standard).getEncryptor(); Encryptor encryptor = new EncryptionInfo(EncryptionMode.standard).getEncryptor();
encryptor.confirmPassword(writeWorkbookHolder.getPassword()); encryptor.confirmPassword(writeWorkbookHolder.getPassword());
OPCPackage opcPackage = null;
OutputStream outputStream = null;
try {
opcPackage = OPCPackage.open(file, PackageAccess.READ_WRITE);
outputStream = encryptor.getDataStream(fileSystem);
opcPackage.save(outputStream);
} finally {
if (opcPackage != null) {
opcPackage.close();
}
if (outputStream != null) {
outputStream.close();
}
try (OPCPackage opcPackage = OPCPackage.open(file, PackageAccess.READ_WRITE); try (OPCPackage opcPackage = OPCPackage.open(file, PackageAccess.READ_WRITE);
OutputStream outputStream = encryptor.getDataStream(fileSystem)) { OutputStream outputStream = encryptor.getDataStream(fileSystem)) {
opcPackage.save(outputStream); opcPackage.save(outputStream);

Loading…
Cancel
Save