杏花怎酿酒 2022-08-18 21:37 采纳率: 55%
浏览 14

springboot vue 下载文件 没有文件就下载的是空文件

springboot vue 下载文件 没有文件就下载的是空文件

前端 vue

 // 下载文件
    download(ref) { // 
      console.log(ref.id)
      const id = ref.id
      getBackupDown(id).then((res) => {
        if (res.data === null) {
          this.$message.error('下载失败')
        } else {
          this.$message.success('下载成功')

          const blob = new Blob([res.data], {type: 'application;charset=utf-8'})
          let link = document.createElement('a')
          let href = window.URL.createObjectURL(blob)//下载链接
          link.href = href
          link.text = '下载'
          link.download = ref.time + '.tar.gz' 
          document.body.appendChild(link)
          link.click() //点击下载
          document.body.removeChild(link)//下载完成移除元素
          window.URL.revokeObjectURL(href) //释放blob对象
        }
      }).catch(err => {
        this.$message.error('文件已删除')
      })
    },

export function getBackupDown (id) {
  return axios({
    url: 'main/selectBackup/download/'+id,
    method: 'get',
    responseType: 'blob'
  })
} 


后端 boot controller

    @GetMapping("/download/{id}")
    public void downLoad(HttpServletResponse response, @PathVariable("id") int id) throws UnsupportedEncodingException {
        Backup backup = backupService.getById(id);
        response.setContentType("application/octet-stream");
        UploadUtil.down(response,backup.getPath());
    }

后端 service


@Slf4j
public class UploadUtil {
    public static void down(HttpServletResponse res, String pathAddress) throws UnsupportedEncodingException {
        
        File file=new File(pathAddress);
        if (!file.exists()){
            log.error("没有此文件");
            return;
        }
        String fileName = file.getName();

        res.setHeader("content-type", "application/octet-stream");
        res.setContentType("application/octet-stream");
        res.setHeader("Content-Disposition", "attachment;filename=" + new String(fileName.getBytes("utf-8"),"iso-8859-1"));

        byte[] buff = new byte[1024];
        FileInputStream bis = null;
        ServletOutputStream os = null;
        try {
            os = res.getOutputStream();
            bis = new FileInputStream(file);
            int readTmp = 0;
            while ((readTmp = bis.read(buff)) != -1) {
                os.write(buff, 0, readTmp);
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (bis != null) try {
                bis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        System.out.println("success");
    }

}


  • 写回答

1条回答 默认 最新

  • 猿小白888 2022-08-19 09:50
    关注

    service里面你把响应头的设置放到这个上面

    img


    然后在这里面把空文件写入响应里面,return出去,试试行不行。
    若有帮助请采纳。

    评论

报告相同问题?

问题事件

  • 创建了问题 8月18日

悬赏问题

  • ¥20 python忆阻器数字识别
  • ¥15 无法输出helloworld
  • ¥15 高通uboot 打印ubi init err 22
  • ¥20 PDF元数据中的XMP媒体管理属性
  • ¥15 R语言中lasso回归报错
  • ¥15 网站突然不能访问了,上午还好好的
  • ¥15 有没有dl可以帮弄”我去图书馆”秒选道具和积分
  • ¥15 semrush,SEO,内嵌网站,api
  • ¥15 Stata:为什么reghdfe后的因变量没有被发现识别啊
  • ¥15 振荡电路,ADS仿真