Excel文件加密的兩種方式

我們經常會遇到導出的數據需要安全和需要脫敏的場景,這種場景下如何做呢,這裏我們使用EasyExcel來實現,有以下兩種方案

使用EasyExcel配合Zip4j將文件加密爲zip

Zip4j的壓縮選項更多

    //生成密碼壓縮文件
    private static File getZipFile(File  file,char [] pwd) throws ZipException {
        ZipFile zipFile = new ZipFile(UUID.randomUUID().toString()+".zip",pwd);
        ZipParameters parameters = new ZipParameters();
        // 壓縮級別
        parameters.setCompressionMethod(CompressionMethod.DEFLATE);
        parameters.setCompressionLevel(CompressionLevel.NORMAL);
        parameters.setEncryptFiles(true);
        parameters.setEncryptionMethod(EncryptionMethod.ZIP_STANDARD);
        parameters.setAesKeyStrength(AesKeyStrength.KEY_STRENGTH_256);
        zipFile.addFile(file,parameters);
        return zipFile.getFile();
    }
    //轉換File爲MultipartFile
        public static MultipartFile getMultipartFile(File file,String pwd) throws Exception{
        File zipFile =   getZipFile(file,pwd.toCharArray());
        MultipartFile multipartFile = new CommonsMultipartFile(ExcelUtil.createFileItem(zipFile));
        if (file != null && file.exists()) {
            file.delete();
        }
        if (zipFile != null && zipFile.exists()) {
            zipFile.delete();
        }
        return multipartFile;
    }
 
        /**
     *  multipartFile.getInputStream()
     * @description: 將輸入流輸出到頁面
     */
    public static void writeFile(HttpServletResponse resp, InputStream inputStream) {
        OutputStream out = null;
        try {
            out = resp.getOutputStream();
            int len = 0;
            byte[] b = new byte[1024];
            while ((len = inputStream.read(b)) != -1) {
                out.write(b, 0, len);
            }
            out.flush();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (out != null) {
                    out.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

使用的pom,請使用最新版,之前的老版本會有安全問題

        <dependency>
            <groupId>net.lingala.zip4j</groupId>
            <artifactId>zip4j</artifactId>
            <version>2.10.0</version>
        </dependency>

直接使用EasyExcel的password爲Excel文件加密

        ExcelWriter writer = null;
        OutputStream outputStream = null;
        try {
            String fileName = UUID.randomUUID() + excelTypeEnum.getValue();
            prepareResponse(response, fileName);
            outputStream = response.getOutputStream();
            writer = EasyExcel.write(outputStream)
                    .excelType(excelTypeEnum)
                    .password(pwd)
                    .build();
            WriteSheet sheet = EasyExcel.writerSheet(sheetName)
                    .head(dataList.get(0).getClass())
                    .build();
            writer.write(dataList, sheet);

        } finally {
            if (writer != null) {
                writer.finish();
            }
            if (outputStream != null) {
                outputStream.close();
            }
        }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章