从后台下载zip文件

从后端下载zip文件压缩包

vue文件处理

下面展示对后台传回的 数据流的处理。

Vue.prototype.$exportClick = function(res, name = "下载.zip") {
      const content = res;
      const blob = new Blob([content]);
      const fileName = name;
      if ("download" in document.createElement("a")) {
        // 非IE下载
        const elink = document.createElement("a");
        elink.download = fileName;
        elink.style.display = "none";
        elink.href = URL.createObjectURL(blob);
        document.body.appendChild(elink);
        elink.click();
        URL.revokeObjectURL(elink.href); // 释放URL 对象
        document.body.removeChild(elink);
      } else {
        // IE10+下载
        navigator.msSaveBlob(blob, fileName);
      }
    };

后台传回zip数据流

获取 zip数据流

public ResponseInfo exportPhoto(HttpServletResponse response) throws IOException {
        //获取员工照片map
        Map<String, String> photoMap = personService.findPhotoInfo();
        response.setContentType("application/x-download;charset=UTF-8");
        response.addHeader("Content-disposition", "filename=employee.xls");
        ZipOutputStream zipOut = new ZipOutputStream(response.getOutputStream());
        Iterator<Map.Entry<String, String>> entryIterator = photoMap.entrySet().iterator();
        FileInputStream fis;
        ZipEntry zipEntry;
        File file;
        Map.Entry<String, String> entry;
        while (entryIterator.hasNext()) {
            entry = entryIterator.next();
            zipEntry = new ZipEntry(entry.getKey() + ".jpg");
            zipOut.putNextEntry(zipEntry);
            file = new File(realPathResolver.get(entry.getValue()));
            try {
                fis = new FileInputStream(file);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
                continue;
            }
            byte[] bytes = new byte[1024];
            int len = 0;
            while ((len = fis.read(bytes)) != -1) {
                zipOut.write(bytes, 0, len);
            }
            fis.close();
        }
        zipOut.close();
        return new ResponseInfo();
    }
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值