• vue+axios+springboot文件下载


    //前台代码
    <el-button size="medium" type="primary" @click="downloadFile">Test</el-button>
    //js函数
    downloadFile(){
          this.axios({
            method: "get",
            url: '/api/downloadFile',
            responseType: 'blob',
            headers: {
              Authorization: localStorage.getItem("token")
            }
          })
            .then(response => {
           //文件名 文件保存对话框中的默认显示
             let fileName = 'test.txt';
             let data = response.data;
             if(!data){
               return
             }
             console.log(response);
          //构造a标签 通过a标签来下载
             let url = window.URL.createObjectURL(new Blob([data]))
             let a = document.createElement('a')
             a.style.display = 'none'
             a.href = url
           //此处的download是a标签的内容,固定写法,不是后台api接口
             a.setAttribute('download',fileName)
             document.body.appendChild(a)
             //点击下载
             a.click()
             // 下载完成移除元素
             document.body.removeChild(a);
             // 释放掉blob对象
             window.URL.revokeObjectURL(url);
            })
            .catch(response => {
              this.$message.error(response);
            });
        },
    //后台代码

    @RequestMapping(value = "/downLoad", method = RequestMethod.GET)
    public static final String downLoad(HttpServletRequest req, HttpServletResponse res){
      Map<String, Object> reMap = new HashMap<>();
      String fileName = "aaa.txt";
      String path = "f:/svss/";
      String filepath = path+fileName;
      OutputStream os = null;
      InputStream is = null;
      try {
        // 清空输出流
        res.reset();
        res.setCharacterEncoding("utf-8");
        res.setContentType("application/octet-stream");
        res.setHeader("Content-Disposition",
          "attachment;filename=" + new String(fileName.getBytes("UTF-8"), "ISO8859-1"));
        // 读取流
        File f = new File(filepath);
        is = new FileInputStream(f);
        if (is == null) {
          reMap.put("msg", "下载附件失败");
        }
        ServletOutputStream sout = res.getOutputStream();
        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;
        bis = new BufferedInputStream(is);
        bos = new BufferedOutputStream(sout);
        byte[] buff = new byte[2048];
        int bytesRead;
        while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
          bos.write(buff, 0, bytesRead);
        }
        bos.flush();
        bos.close();
        bis.close();
        is.close();
        os.close();
      } catch (Exception e) {
      reMap.put("msg", "下载附件失败,error:" + e.getMessage());
      }

      String str = JsonUtil.map2Json(reMap);
      return str;
    }


  • 相关阅读:
    DOM
    JS方法
    边界与边框,列表与方块
    for 练习
    背景与前景温习
    AD域账号验证
    邮件发送案例
    获取每个月最后一天的小技巧
    SQL 执行顺序
    常用下载地址
  • 原文地址:https://www.cnblogs.com/xuchao0506/p/12803316.html
Copyright © 2020-2023  润新知