林谿 2021-10-28 10:13 采纳率: 52.9%
浏览 28
已结题

多个二维码打包zip并返回前端数据流下载,解压失败

serviceImpl代码


@Override
    public void QRCodePage(MPage page, TbJmzzxqDto param) throws IOException, WriterException {
        String fullDirectory = filesPath + "/" + new SimpleDateFormat("yyyyMMddHHmmss").format(new Date().getTime()).toString();
        System.out.println(fullDirectory);
        File dir = new File(fullDirectory);
        if (!dir.exists()) {// 判断目录是否存在
            dir.mkdir();
        }
        //得到数据
        MPage<TbJmzzxqDto> mPage = this.residentialPageList(page, param);
        List<TbJmzzxqDto> dataList = mPage.getRecords();
        //循环生成二维码图片
        for (TbJmzzxqDto jmzzxqDto : dataList) {
            //二维码内容
            String content = "tb_jmzzxq," + jmzzxqDto.getId();
            //生成二维码
            QRCodeUtil.generateQRCodeImage(content, fullDirectory + "/" + jmzzxqDto.getId() + ".png");
        }
        ZipUtil zca = new ZipUtil(filesPath+"\\zzxq.zip");
        zca.compressExe(fullDirectory);
    }

ZipUtil代码:

private File zipFile;
    /**
     * 压缩文件构造函数
     * @param finalFile 最终压缩生成的压缩文件:目录+压缩文件名.zip
     */
    public ZipUtil(String finalFile) {
        zipFile = new File(finalFile);
    }

    /**
     * 执行压缩操作
     * @param srcPathName 需要被压缩的文件/文件夹
     */
    public void compressExe(String srcPathName) {
        System.out.println("srcPathName="+srcPathName);

        File srcdir = new File(srcPathName);
        if (!srcdir.exists()){
            throw new RuntimeException(srcPathName + "不存在!");
        }

        Project prj = new Project();
        Zip zip = new Zip();
        zip.setProject(prj);
        zip.setDestFile(zipFile);
        FileSet fileSet = new FileSet();
        fileSet.setProject(prj);
        fileSet.setDir(srcdir);
        zip.addFileset(fileSet);
        zip.execute();
    }

压缩成功也可以解压
Controller代码:

@ApiOperation(value = "下载zip", notes = "下载zip")
    @GetMapping("/v1/QRCodePage")
    public void QRCodePage(MPage page, TbJmzzxqDto param, HttpServletResponse response) throws Exception {
        tbJmzzxqService.QRCodePage(page, param);
        byte[] buffer = null;
        response.setContentType("application/zip");
        response.setHeader("Content-Disposition", "attachment; filename=住宅小区.zip");
        ServletOutputStream os = response.getOutputStream();
        File file = new File(filesPath + "\\zzxq.zip");
        FileInputStream fis = new FileInputStream(file);
        ByteArrayOutputStream bos = new ByteArrayOutputStream(1000);
        byte[] b = new byte[1024];
        int n;
        while ((n = fis.read(b)) != -1) {
            bos.write(b, 0, n);
        }
        fis.close();
        bos.close();
        buffer = bos.toByteArray();
        os.write(buffer);
        os.flush();
        os.close();
    }

vue代码:

handleTableQr(){
      let page={
        current:this.current,
        size:this.size
      }
      let param = Object.assign(this.queryInfo, page);
      this.DZGET(this.APIURL.ZZXQ_QR, param).then((res) => {
        const link = document.createElement("a");
        let blob = new Blob([res]);
        link.style.display = "none";
        link.href = URL.createObjectURL(blob);
        link.setAttribute("download", "住宅小区.zip");
        document.body.appendChild(link);
        link.click();
        document.body.removeChild(link);
      });
    }

下载成功,解压失败

img

img


求知道的人指教是什么问题

  • 写回答

1条回答 默认 最新

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 11月26日
  • 创建了问题 10月28日

悬赏问题

  • ¥30 模拟电路 logisim
  • ¥15 PVE8.2.7无法成功使用a5000的vGPU,什么原因
  • ¥15 is not in the mmseg::model registry。报错,模型注册表找不到自定义模块。
  • ¥15 安装quartus II18.1时弹出此error,怎么解决?
  • ¥15 keil官网下载psn序列号在哪
  • ¥15 想用adb命令做一个通话软件,播放录音
  • ¥30 Pytorch深度学习服务器跑不通问题解决?
  • ¥15 部分客户订单定位有误的问题
  • ¥15 如何在maya程序中利用python编写领子和褶裥的模型的方法
  • ¥15 Bug traq 数据包 大概什么价