The version problem of the password file generated by poi export excel

When using poi to generate an encrypted file of excel, the following exception will occur when calling the following method -----encryptOutPutStream.close() java.io.FileNotFoundException: /home/wftapp/eppc/services/eppc-service/temp/poifiles/encrypted_package3872927755796981223crypt (No such file or dire story)

This problem occurs because the poi version is too low. Local use of 3.15 and 3.17 does not work, and 4.0.1 can be used to solve it.

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

Guess you like

Origin blog.csdn.net/weixin_42740540/article/details/127998853