java 将图片放在压缩包中尽心下载

需求 在下载订单图片的时候需要将图片放在压缩包中进行下载,如下:

 @SneakyThrows
    @GetMapping("/downloadJyzByIdZip")
    @ApiOperation(value = "根据uuidStr下载文件", notes = "根据uuidStr下载文件(uuidStr为文件名、uuid的拼接)")
    @ApiImplicitParam(name = "uuidStr", value = "文件名和uuid字符串", required = true, dataType = "string", paramType = "path")
    public R downloadJyzByIdZip(String orderId, HttpServletResponse response) {
        //根据订单编号查询图片的ID
        String newManagementRightCert = orderMapper.getInfoDowload(orderId);
        if (newManagementRightCert ==null){
            return R.ok("图片未上传,请联系管理员!");
        }else {
            String [] arr = newManagementRightCert.split(",");
            //将图片的存放位置找出 放在list数组中
            List<String> voList = new ArrayList<>();
            if (arr.length>0){
                for (int i = 0; i < arr.length; i++) {
                    IaFileStore iaFileStore = iaFileStoreMapper.selectOne(new QueryWrapper<IaFileStore>().eq("id", arr[i]));
                    if (null ==iaFileStore){
                        return R.ok(voList);
                    }else {
                        IaFiles iaFiles = fileService.getOne(new QueryWrapper<IaFiles>().eq("file_name",iaFileStore.getName()));
                        fileService.getFileUrl(iaFiles.getUuid());
                        voList.add(fileService.getFileUrl(iaFiles.getUuid()));
                    }
                }
            }else {
                return R.ok("文件下载失败,id不能为空!");
            }
            //对图片进行压缩
            //判断list中有多少图片
            System.out.println(voList);
            if (voList !=null && voList.size()>0){
                /*批量下载*/
                try {
                    String downloadFilename="";
                    String nowTimeString = String.valueOf(System.currentTimeMillis());
                    downloadFilename = "经营权证.zip";//压缩包文件的名称
                    downloadFilename = URLEncoder.encode(downloadFilename, "UTF-8");//转换中文否则可能会产生乱码
                    response.setContentType("application/octet-stream");// 指明response的返回对象是文件流
                    response.setHeader("Content-Disposition", "attachment;filename=" + downloadFilename);// 设置在下载框默认显示的文件名
                    ZipOutputStream zos = new ZipOutputStream(response.getOutputStream());

                    FileInputStream ips = null;
                    File file =new File("");
                    String url="";
                    byte[] buffer = new byte[1024];
                    int r=0;
                    for (int i = 0; i < voList.size(); i++) {
                        if(voList.get(i)!=null){
                            zos.putNextEntry(new ZipEntry("图片"+".jpg"));
                            url=voList.get(i);
                            //获取图片存放路径
                            file = new File(url);
                            System.out.println(file);
                            if(file.exists()) {
                                ips = new FileInputStream(file);
                                response.setContentType("multipart/form-data");
                                while ((r = ips.read(buffer)) != -1){
                                    zos.write(buffer, 0, r);
                                }
                                if (null != ips) {
                                    ips.close();
                                }
                            }else {
                                System.out.println("为空");
                            }
                        }
                    }
                    zos.flush();
                    zos.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            return R.ok(voList,"文件下载成功!");
        }
    }

以上市在实际工作中的代码,自己在学习,分享记录一下,与大家共享,如有不到之处,多多指教

以上若有侵权,请联系删除

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值