工作中文件下载是一个非常常见的功能,但请求头设置却经常容易忘记。这里写一个样例供大家参考。这里设置文件编码格式,主要防止下载文件中文名乱码。
一、文件下载
1、主要设置 下载的类型
2、设置下载文件的名称
@GetMapping("/downloadFile")
public void downloadFile(HttpServletResponse response,@RequestParam("fileContent") Integer fileContent) {
File file = null;
file = new File(fileContentMap.get(fileContent));
try (ServletOutputStream os = response.getOutputStream();
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));){
byte[] buff = new byte[1024];
String fileName = new String(file.getName().getBytes(), "ISO8859-1");
response.setHeader("content-type", "application/octet-stream;charset=ISO8859-1");
response.setContentType("application/octet-stream;charset=ISO8859-1");
response.setHeader("Content-dispositi