SpringBoot之文件下载

 1 package org.springboot.controller;
 2 
 3 import org.springboot.constant.Constant;
 4 import org.springframework.stereotype.Controller;
 5 import org.springframework.web.bind.annotation.RequestMapping;
 6 
 7 import javax.servlet.http.HttpServletRequest;
 8 import javax.servlet.http.HttpServletResponse;
 9 import java.io.*;
10 import java.net.URLEncoder;
11 
12 /**
13  * @Auther:GongXingRui
14  * @Date:2018/12/24
15  * @Description:
16  **/
17 @Controller
18 public class FileDownload {
19 
20         //实现Spring Boot 的文件下载功能,映射网址为/download
21         @RequestMapping("/download")
22         public String downloadFile(HttpServletRequest request,
23                                    HttpServletResponse response) throws UnsupportedEncodingException {
24 //            String picPath = "C:/Temp/pic";
25             String picPath = Constant.CONFIG_PROPERTIES.getProperty("download.path");
26 
27             // 获取指定目录下的第一个文件
28             File scFileDir = new File(picPath);
29             File TrxFiles[] = scFileDir.listFiles();
30             System.out.println(TrxFiles[0]);
31             String fileName = TrxFiles[0].getName(); //下载的文件名
32 
33             // 如果文件名不为空,则进行下载
34             if (fileName != null) {
35                 //设置文件路径
36                 File file = new File(picPath, fileName);
37 
38                 // 如果文件名存在,则进行下载
39                 if (file.exists()) {
40                     // 配置文件下载
41                     response.setHeader("content-type", "application/octet-stream");
42                     response.setContentType("application/octet-stream");
43                     // 下载文件能正常显示中文
44                     response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
45 
46                     // 实现文件下载
47                     byte[] buffer = new byte[1024];
48                     FileInputStream fis = null;
49                     BufferedInputStream bis = null;
50                     try {
51                         fis = new FileInputStream(file);
52                         bis = new BufferedInputStream(fis);
53                         OutputStream os = response.getOutputStream();
54                         int i = bis.read(buffer);
55                         while (i != -1) {
56                             os.write(buffer, 0, i);
57                             i = bis.read(buffer);
58                         }
59                         System.out.println("Download the file successfully!");
60                     }
61                     catch (Exception e) {
62                         System.out.println("Download the file failed!");
63                     }
64                     finally {
65                         if (bis != null) {
66                             try {
67                                 bis.close();
68                             } catch (IOException e) {
69                                 e.printStackTrace();
70                             }
71                         }
72                         if (fis != null) {
73                             try {
74                                 fis.close();
75                             } catch (IOException e) {
76                                 e.printStackTrace();
77                             }
78                         }
79                     }
80                 }
81             }
82             return null;
83         }
84 
85 
86 }
原文地址:https://www.cnblogs.com/gongxr/p/10189256.html