poi导出excel生成密码文件的版本问题

在使用poi生成excel的加密文件时,调用如下方法-----encryptOutPutStream.close()时会出现以下异常java.io.FileNotFoundException: /home/wftapp/eppc/services/eppc-service/temp/poifiles/encrypted_package3872927755796981223crypt (No such file or directory)

此问题的出现是因为poi版本太低的问题,本地使用3.15、3.17都不行,使用4.0.1即可解决。

 public void writeStreamWithPassword(String password) {
        FileOutputStream fos = null;
        POIFSFileSystem fs = null;
        OutputStream encryptOutPutStream = null;
        try {
            EncryptionInfo info = new EncryptionInfo(EncryptionMode.standard);
            Encryptor enc = info.getEncryptor();
            //设置密码
            enc.confirmPassword(password);
            //加密文件
            fs = new POIFSFileSystem();
            fos = new FileOutputStream(filePath);
            encryptOutPutStream = enc.getDataStream(fs);
            workbook.write(encryptOutPutStream);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if(encryptOutPutStream != null){
                try {
                    encryptOutPutStream.close();
                    fs.writeFilesystem(fos);
                } catch (IOException e) {
                    LOG.error("writeStreamWithPassword err1",e);
                    e.printStackTrace();
                }
            }
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException e) {
                    LOG.error("writeStreamWithPassword err2",e);
                    e.printStackTrace();
                }
            }
            if (fs != null) {
                try {
                    fs.close();
                } catch (IOException e) {
                    LOG.error("writeStreamWithPassword err3",e);
                    e.printStackTrace();
                }
            }
        }
    }

猜你喜欢

转载自blog.csdn.net/weixin_42740540/article/details/127998853