Spring MVC 文件下载
2018-04-14 18:33:33
850次阅读
0个评论
/**
* 下载
* @param model
* @param request
* @param response
* @return
* @throws Exception
*/
@RequestMapping(params="method=download", method=RequestMethod.GET)
public String download(ModelMap model,String dirName,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
//文件路径
String path = "c://1.rar";
String fileName = "rar";//获取文件名,含后缀
File file = new File(path);
response.reset();
response.setContentType("application/octet-stream; charset=UTF-8");
fileName = URLEncoder.encode(fileName, "UTF-8");//IE浏览器
response.addHeader("Content-Disposition","attachment;filename="+fileName);
response.setContentLength((int) file.length());
byte[] buffer = new byte[4096];
BufferedOutputStream output = null;
BufferedInputStream input = null;
// 写缓冲区:
try {
output = new BufferedOutputStream(response.getOutputStream());
input = new BufferedInputStream(new FileInputStream(file));
int n = (-1);
while ((n = input.read(buffer, 0, 4096)) > -1) {
output.write(buffer, 0, n);
}
response.flushBuffer();
}
catch (Exception e) {
} // maybe user cancelled download
finally {
if (input != null) input.close();
if (output != null) output.close();
}
return null;
}
00
相关话题
- 解决IE浏览器不支持Spring MVC 文件下载的问题
- 解决spring mvc 返回json对象时ie浏览器访问会出现下载文件弹出框
- Spring下载地址
- Nginx 设置 X-Accel-Redirect 控制文件下载
- 基于Nginx XSendfile+SpringMVC进行文件下载
- Apache的commons-net实现FTP的文件上传下载
- nginx 多组件安装及secure_link配置与文件下载防盗链的使用
- spring配置文件中bean的属性name与id的区别
- Spring常用注解
- spring boot集成Hibernate配置
- Spring 自带工具类
- Spring+redis实现session集群
- Spring获取Bean的自身id
- Spring Configuration动态绑定bean id
- 文件流式上传