提交 | 用户 | 时间
|
1ac2bc
|
1 |
package cn.stylefeng.guns.sys.core.util; |
懒 |
2 |
|
|
3 |
import cn.stylefeng.roses.core.util.FileUtil; |
|
4 |
|
|
5 |
import javax.servlet.http.HttpServletResponse; |
|
6 |
import java.io.BufferedOutputStream; |
|
7 |
import java.io.OutputStream; |
|
8 |
import java.net.URLEncoder; |
|
9 |
|
|
10 |
/** |
|
11 |
* 下载文件 |
|
12 |
* |
|
13 |
* @author fengshuonan |
|
14 |
* @Date 2019/8/7 23:14 |
|
15 |
*/ |
|
16 |
public class FileDownload { |
|
17 |
|
|
18 |
/** |
|
19 |
* @param response |
|
20 |
* @param filePath 文件完整路径(包括文件名和扩展名) |
|
21 |
* @param fileName 下载后看到的文件名 |
|
22 |
* @return 文件名 |
|
23 |
*/ |
|
24 |
public static void fileDownload(final HttpServletResponse response, String filePath, String fileName) throws Exception { |
|
25 |
byte[] bytes = FileUtil.toByteArray(filePath); |
|
26 |
fileName = URLEncoder.encode(fileName, "UTF-8"); |
|
27 |
response.reset(); |
|
28 |
response.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\""); |
|
29 |
response.addHeader("Content-Length", "" + bytes.length); |
|
30 |
response.setContentType("application/octet-stream;charset=UTF-8"); |
|
31 |
OutputStream outputStream = new BufferedOutputStream(response.getOutputStream()); |
|
32 |
outputStream.write(bytes); |
|
33 |
outputStream.flush(); |
|
34 |
outputStream.close(); |
|
35 |
response.flushBuffer(); |
|
36 |
} |
|
37 |
|
|
38 |
} |