SpringBoot+vue实现文件下载

简介: SpringBoot+vue实现文件下载

后端代码:

    @RequestMapping("/download/test")
    public void download(HttpServletResponse response,@RequestBody ProjectHistory projectHistory){
        response.setContentType("application/octet-stream");
        response.setHeader("Content-Disposition", "attachment; filename=license.lic");
 
        ProjectHistory serviceById = historyService.getById(projectHistory.getId());
        ///文件路径
        String licenseFilePath = serviceById.getLicenseFile();
 
        try {
            FileInputStream is = new FileInputStream(licenseFilePath);
            byte[] temp = new byte[is.available()];
            is.read(temp);
            response.getOutputStream().write(temp);
            is.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

前端代码:

<template slot-scope="scope">
<el-button @click="download(scope.row.id)" type="text" size="small">下载文件</el-button>
</template>
async download(id){
  const res = await this.downFile('www.baidu.com',{id:'00'}){
    let blob = new Blob([res.data]);
    let link = document.createElement('a');
    link.href = window.URL.createObjectURL(blob);
    link.download = '文件名称.txt';
    link.click();
    link.remove();
  }
}
downFile(url,params){
  const result = axios.post(url,params,{responseType:'blob'}).then(res =>{
    return res;
  });
  return result;
}

 

相关文章
|
18天前
|
Java
Springboot文件下载跨域问题解决方案
Springboot文件下载跨域问题解决方案
springboot 各种文件下载方式(最全)
springboot 各种文件下载方式(最全)
2361 0
|
Java API Spring
spring boot中Excel文件下载踩坑大全
spring boot中Excel文件下载踩坑大全
1227 2
spring boot中Excel文件下载踩坑大全
|
11月前
|
Java
Java小白翻身-基于SpringBoot的文件下载系统 3
Java小白翻身-基于SpringBoot的文件下载系统 3
springboot 文件下载
springboot 文件下载
|
Java Spring
spring boot 实现文件下载
主要介绍了spring boot 实现文件下载
|
存储 Java
SpringMVC,SpringBoot文件下载
前言 最近严查security, 导致原来暴露出去的s3不能用了,不允许public的s3,暂时的折中方案是自己做跳转。于是需要在SpringMVC中实现文件下载功能。 关于文件存储的设计 文件存储通常用作对象存储,业界标准就是AWS s3, 国内的七牛也差不多。
1382 0
SpringBoot SpringMVC实现文件下载
SpringBoot SpringMVC实现文件下载 @RequestMapping(value = "/download", method = RequestMethod.
1065 0
|
18天前
|
Java Linux
Springboot 解决linux服务器下获取不到项目Resources下资源
Springboot 解决linux服务器下获取不到项目Resources下资源
|
25天前
|
Java API Spring
SpringBoot项目调用HTTP接口5种方式你了解多少?
SpringBoot项目调用HTTP接口5种方式你了解多少?
79 2