小功能实现(十五)POST请求,向后端传递参数,并获取返回的文件流(vue)

前言:

  • 简单的文件流,前端只需要一个a标签即可
  • 但这次需要先传递参数,然后后端处理后再返回文件流
  • 总体功能是接受前端传递的数组,然后将文件批量打包,并返回文件流给前端

步骤:

  • 导入axios
import axios from 'axios';
  • 前端发送请求
//参数分别为请求地址和携带参数,这里传的是字符串格式的数组,太长了所以用POST请求
axios.post(process.env.ADMIN_API + '/gim2/downLoadE3ms.do', list_down, { responseType: 'blob' })
  .then(response => {
    const url = window.URL.createObjectURL(new Blob([response.data]));
    const link = document.createElement('a');
    link.href = url;
    link.setAttribute('download', '三维成果.zip');
    document.body.appendChild(link);
    link.click();
    document.body.removeChild(link);
    window.URL.revokeObjectURL(url);
    this.listhbnjm = [];
    this.$refs.multipleTable.clearSelection();
  })
  .catch(error => {
    console.error("下载失败");
  });
  • 后端下载,Controller和ServiceImpl
//接收参数,并返回文件流
@RequestMapping(value = "downLoadE3ms.do", method = RequestMethod.POST)
public void downLoadE3ms(@RequestBody String evEntitys, HttpServletResponse response) throws Exception {
    evGimcheckTaskService.downLoadE3ms(evEntitys, response);
}
	public void downLoadE3ms(String evEntitys, HttpServletResponse response) throws Exception {
        List<EvEntity> evEntities = JSONObject.parseArray(evEntitys, EvEntity.class);
        ArrayList<Map<String,String>> pathList = new ArrayList<>();
        String path="";
        for (EvEntity evEntity : evEntities) {
            String gimName=evEntity.getGimName().substring(0,evEntity.getGimName().indexOf("."));
            Map<String,String> pathMap=new HashMap<>();
            path="...";
            pathMap.put("path",path);
            pathMap.put("gimName",gimName);
            pathList.add(pathMap);
        }
        FileUtil.downE3ms(pathList,response);
    }
  • 下载核心代码
public static void downE3ms(List<Map<String,String>> pathList, HttpServletResponse response) throws IOException {
    byte[] buf = new byte[1024];
    BufferedOutputStream bos = null;
    ZipOutputStream out = null;
    try {
        bos = new BufferedOutputStream(response.getOutputStream());
        //响应到浏览器
        response.reset();
        response.setContentType("application/x-msdownload");
        response.setCharacterEncoding("utf-8");
        response.setHeader("Content-Disposition","attachment;filename="+"阿弥陀佛.zip");
        out = new ZipOutputStream(bos);
        for (Map<String,String> path : pathList) {
            File zip = ZipUtil.zip(path.get("path"));

            FileInputStream in = new FileInputStream(zip);
            out.putNextEntry(new ZipEntry(path.get("gimName")+".zip"));
            int len = -1;
            while ((len=in.read(buf))!=-1){
                out.write(buf,0,len);
            }
            in.close();
            zip.delete();
        }
    } catch (IOException e) {
        throw new RuntimeException(e);
    } finally {
        if (out!=null)
            out.close();
        if (bos!=null)
            bos.close();
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值