Python语言技术文档

微信小程序技术文档

php语言技术文档

jsp语言技术文档

asp语言技术文档

C#/.NET语言技术文档

html5/css技术文档

javascript

点击排行

您现在的位置:首页 > 技术文档 > jsp框架

Spring_MVC的文件下载实例详解

来源:中文源码网    浏览:283 次    日期:2024-05-04 12:16:41
【下载文档:  Spring_MVC的文件下载实例详解.txt 】


Spring MVC的文件下载实例详解
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 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(body, headers, status);
}
这里需要注意,一般来说下载文件是使用201状态码的,但是IE浏览器不支持,还得我花了很大力气才找出来是那个问题
其中对文件名的处理是为了防止中文以及空格导致文件名乱码
控制器方法
在控制器的那里需要对返回值进行处理
@RequestMapping(value = "/download-backup", method = RequestMethod.GET)
@ResponseBody
public ResponseEntity downloadBackupFile(@RequestParam String fileName) {
byte[] body = backupService.downloadFile(fileName);
return downloadResponse(body, fileName);
}
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

相关内容