Java客户端下载加密Excel

分享一个关于下载加密excel的方法;
核心思想就是将workbook的数据流到内容,通过POIFSFileSystem进行包装加密,最终将数据流写到response流里面;

关于代码中的writer是引用hutool的操作excel的工具类;大家不必在此费脑哦

后续博主可以再写一个完整的demo

  @Override
    public boolean downloadExcel(ExcelWriter writer, String password, String fileName, String fileSuffix) {
        if (Objects.isNull(writer) || StringUtils.isAnyBlank( fileName, fileSuffix)) {
            log.error("ExcelServiceImpl downloadEncryptExcel params is null!");
            return false;
        }

        HttpServletResponse response = ((ServletRequestAttributes) (RequestContextHolder.currentRequestAttributes())).getResponse();
        if (Objects.isNull(response)) {
            log.error("ExcelServiceImpl downloadExcel fail,response is null!");
            return false;
        }


        boolean result = false;
        ServletOutputStream out = null;

        try {
//            Stri
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
如果你想使用 Java 读取加密Excel 文件,你需要先知道 Excel 文件的加密方式,以便使用相应的工具进行解密。一般来说,Excel 文件的加密方式有两种:密码保护和文件加密。 如果是密码保护,你可以使用 Apache POI 库来读取 Excel 文件。具体的步骤如下: 1. 加载 Excel 文件: ``` File file = new File("test.xlsx"); Workbook workbook = WorkbookFactory.create(file, "password"); ``` 2. 获取 Excel 文件中的数据: ``` Sheet sheet = workbook.getSheetAt(0); for (Row row : sheet) { for (Cell cell : row) { System.out.println(cell.getStringCellValue()); } } ``` 注意,这里的 "password" 参数是打开 Excel 文件时需要输入的密码。 如果是文件加密,你可以使用第三方工具或者 Java Cryptography Extension (JCE) 来解密文件。具体的步骤如下: 1. 使用 JCE 获取密钥: ``` SecretKeySpec key = new SecretKeySpec("password".getBytes(), "AES"); ``` 2. 解密 Excel 文件: ``` File encryptedFile = new File("test.xlsx"); File decryptedFile = new File("test-decrypted.xlsx"); Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); cipher.init(Cipher.DECRYPT_MODE, key, new IvParameterSpec(new byte[16])); try (FileInputStream in = new FileInputStream(encryptedFile); FileOutputStream out = new FileOutputStream(decryptedFile)) { byte[] buffer = new byte[1024]; int len; while ((len = in.read(buffer)) != -1) { out.write(cipher.update(buffer, 0, len)); } out.write(cipher.doFinal()); } ``` 在解密完成后,你可以使用 Apache POI 库来读取解密后的 Excel 文件。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值