vue+Springboot下载文件

前端代码
1
<el-button type="primary" @click="dowload2('1662023440868上传测试.jpg')">下载</el-button>

  js代码

1
2
3
4
5
6
7
8
9
dowload2(msg){
         let href = "http://localhost:9090/myFiles/downloadFile?flag="+msg;
         let downloadElement = document.createElement('a')
         downloadElement.style.display = 'none'
         downloadElement.href = href
         document.body.appendChild(downloadElement)
         // 点击下载
         downloadElement.click()
     },

 后端代码 方法1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
@RequestMapping("/downloadFile")
   public void downloadFiles(@RequestParam("flag") String downUrl, HttpServletRequest request, HttpServletResponse response) {
       downUrl = "D:/FileUpload/"+downUrl;
       System.out.println("downUrl========"+downUrl);
       OutputStream outputStream = null;
       InputStream inputStream = null;
       BufferedInputStream bufferedInputStream = null;
       byte[] bytes = new byte[1024];
       File file = new File(downUrl);
       String fileName = file.getName();
       System.out.println("本次下载的文件为" + downUrl);
       // 获取输出流
       try {
           // StandardCharsets.ISO_8859_1 *=UTF-8'
           // response.setHeader("Content-Disposition", "attachment;filename=" +  new String(fileName.getBytes(StandardCharsets.UTF_8), StandardCharsets.ISO_8859_1));
           response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
           // 以流的形式返回文件
           response.setContentType("application/octet-stream;charset=utf-8");
           inputStream = new FileInputStream(file);
           bufferedInputStream = new BufferedInputStream(inputStream);
           outputStream = response.getOutputStream();
           int i = bufferedInputStream.read(bytes);
           while (i != -1) {
               outputStream.write(bytes, 0, i);
               i = bufferedInputStream.read(bytes);
           }
       } catch (IOException e) {
           e.printStackTrace();
       } finally {
           try {
               if (inputStream != null) {
                   inputStream.close();
               }
               if (outputStream != null) {
                   outputStream.close();
               }
               if (bufferedInputStream != null) {
                   bufferedInputStream.close();
               }
           } catch (IOException e) {
               e.printStackTrace();
           }
 
       }
     }
   

  方法2

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
@RequestMapping("/getFile")
    public void getFile(@RequestParam("flag") String flag, HttpServletResponse response) throws UnsupportedEncodingException {
        System.out.println("getFile下载参数flag======"+flag);
        Result res = new Result();
        String path = "";
        //response.setHeader("Content-Disposition", "attachment;filename=" +  URLEncoder.encode(flag, "UTF-8"));
        // 以流的形式返回文件
        response.setContentType("application/octet-stream;charset=utf-8");
        response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(flag, "UTF-8"));
        try{
            System.out.println("下载文件flag====="+flag);
            OutputStream os;
            path = "D:/FileUpload/"+flag;
            byte[] bytes =  FileUtil.readBytes(new File(path));
            os = response.getOutputStream();
            os.write(bytes);
            os.flush();
            os.close();
            System.out.println("path====="+path);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

  pom.xml文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.5.1</version>
        </dependency>
 
        <dependency>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-all</artifactId>
            <version>5.8.5</version>
        </dependency>

  

 

posted @   红尘沙漏  阅读(1114)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
历史上的今天:
2020-09-02 java 合并两个list 并去重
2020-09-02 java使用map去重复
2020-09-02 java中对list集合中的数据按照某一个属性进行分组
2020-09-02 JS实现JSON数组合并和去重
2020-09-02 将两个ListMap中同下标的map去重合并
2020-09-02 Java 将两个Map对象合并为一个Map对象
2020-09-02 强大的动态SQL
点击右上角即可分享
微信分享提示