根据若依系统+minio实现批量下载附件并自动压缩成zip

效果实现:

 

分割!!!!

以下代码参考于

http://t.csdn.cn/4dUmDwg


话不多说 直接从后端开始

0.首先是pom依赖

<dependency>
    <groupId>cn.hutool</groupId>
    <artifactId>hutool-all</artifactId>
    <version>5.5.7</version>
</dependency>

1.后端Controller

ids和tableName为我业务需要(主要是查询数据库的附件表)

    /**
     * 下载多个文件转zip压缩包
     * @param ids
     * @param tableName
     * @param response
     * @throws Exception
     */
    @RequestMapping("/dowloadToZip/{ids}/{tableName}")
    public void dowloadToZip(@PathVariable Long[] ids,@PathVariable String tableName,HttpServletResponse response) throws Exception {
        fileService.dowloadToZip(ids,tableName,response);
    }

2.Service实现层

需要各位自己搜索minio关于下载的代码 也就是获取图片的inputStream流(因为各自代码逻辑不同)


    /**
     * 下载多个文件转zip压缩包
     *
     * @param ids
     * @param tableName
     * @param response
     * @throws Exception
     */
    @Override
    public void dowloadToZip(Long[] ids, String tableName, HttpServletResponse response) throws Exception {
        for (Long id : ids) {
            int i = 0;
            List<Attachment> attachments = attachmentService.attachmentSearch(id.toString(), tableName);
            //如果有附件 进行zip处理
            if (attachments != null && attachments.size() > 0) {
                //被压缩文件流集合
                InputStream[] srcFiles = new InputStream[attachments.size()];
                //被压缩文件名称
                String[] srcFileNames = new String[attachments.size()];
                for (Attachment attachment : attachments) {
                    //以下代码为获取图片inputStream 
                    String url = attachment.getUrl();
                    String[] names = url.split("/");
                    String name = tableName + "/" + names[names.length - 1];
                    GetObjectArgs args = GetObjectArgs.builder().bucket(minioConfig.getBucketName()).object(name).build();
                    InputStream inputStream = client.getObject(args);
                    if (inputStream == null) {
                        continue;
                    }
                    //塞入流数组中
                    srcFiles[i] = inputStream;
                    srcFileNames[i] = attachment.getAttachmentName();
                    i++;
                }
                response.setCharacterEncoding("UTF-8");
                response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode("下载.zip", "UTF-8"));
                //多个文件压缩成压缩包返回
                ZipUtil.zip(response.getOutputStream(), srcFileNames, srcFiles);
            }

        }
    }

后端到这里就结束了,主要注意两个地方


1. ZipUtil是用pom导入依赖

2. 需要各自补充获取图片inputStream的代码 然后放入InputStream[]这个数组中即可

      


再其次回到前端

前端方面更简单

1. 找到前端项目的request.js 查看是否有download方法

如果有这个方法就简单 没有的话 各位自己copy吧!

 2.找到 button批量下载证件 按钮 赋值@click="zipDownload" 并实现download代码块


  • 2
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值