Spring MVC的文件下载实例详解读取文件要下载文件,首先是将文件内容读取进来,使用字节数组存储起来,这里使用spring里面的工具类实现import org.springframework.util.FileCopyUtils; pu...
Spring MVC的文件下载实例详解
读取文件
要下载文件,首先是将文件内容读取进来,使用字节数组存储起来,这里使用spring里面的工具类实现
import org.springframework.util.FileCopyUtils;
public byte[] downloadFile(String fileName) {
byte[] res = new byte[0];
try {
File file = new File(BACKUP_FILE_PATH, fileName);
if (file.exists() && !file.isDirectory()) {
res = FileCopyUtils.copyToByteArray(file);
}
} catch (IOException e) {
logger.error(e.getMessage());
}
return res;
}
这个数组就是文件的内容,后面将输出到响应,供浏览器下载
下载文件的响应
下载文件的响应头和一般的响应头是有所区别的,而这里面还要根据用户浏览器的不同区别对待
我把生成响应的代码封装成了一个方法,这样所有下载响应都可以调用这个方法了,避免重复代码到处写
protected ResponseEntity<byte[]> downloadResponse(byte[] body, String fileName) {
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder
.getRequestAttributes()).getRequest();
String header = request.getHeader("User-Agent").toUpperCase();
HttpStatus status = HttpStatus.CREATED;
try {
if (header.contains("MSIE") || header.contains("TRIDENT") || header.contains("EDGE")) {
fileName = URLEncoder.encode(fileName, "UTF-8");
fileName = fileName.replace("+", "%20"); // IE下载文件名空格变+号问题
status = HttpStatus.OK;
} else {
fileName = new String(fileName.getBytes("UTF-8"), "ISO8859-1");
}
} catch (UnsupportedEncodingException e) {}
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
headers.setContentDispositionFormData("attachment", fileName);
headers.setContentLength(body.length);
return new ResponseEntity<byte[]>(body, headers, status);
}
这里需要注意,一般来说下载文件是使用201状态码的,但是IE浏览器不支持,还得我花了很大力气才找出来是那个问题
其中对文件名的处理是为了防止中文以及空格导致文件名乱码
控制器方法
在控制器的那里需要对返回值进行处理
@RequestMapping(value = "/download-backup", method = RequestMethod.GET)
@ResponseBody
public ResponseEntity<byte[]> downloadBackupFile(@RequestParam String fileName) {
byte[] body = backupService.downloadFile(fileName);
return downloadResponse(body, fileName);
}
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
沃梦达教程
本文标题为:Spring MVC的文件下载实例详解


基础教程推荐
猜你喜欢
- Spring MVC数据绑定方式 2023-06-30
- SpringBoot嵌入式Web容器原理与使用介绍 2023-06-17
- springboot中request和response的加解密实现代码 2022-12-08
- jsp hibernate的分页代码第3/3页 2024-01-11
- 详解http请求中的Content-Type 2023-07-31
- java 解决Eclipse挂掉问题的方法 2024-01-10
- SpringBoot 2.5.5整合轻量级的分布式日志标记追踪神器TLog的详细过程 2023-06-17
- 关于@MapperScan包扫描的坑及解决 2023-04-16
- JSP servlet实现文件上传下载和删除 2023-07-30
- 用javascript制作qq注册动态页面 2023-12-16