springle MVC中如何下载文件呢?
比struts2 下载文件简单得多
先看例子:
@ResponseBody @RequestMapping(value = "/download",produces="application/octet-stream") public byte[] downloadFile(HttpServletRequest request, HttpServletResponse response,String contentType2) throws IOException { byte[]bytes=FileUtils.getBytes4File("D:\\Temp\\cc.jpg"); response.addHeader("Content-Disposition", "inline;filename=\"a.jpg\""); return bytes; }
在浏览器中访问:http://localhost:8080/tv_mobile/video/download
注意:
(1)使用注解@ResponseBody 表示action返回的内容直接作为应答体返回;
(2)通过produces 设置返回的content type;
(3)直接返回要下载的文件的二进制数组
参考:http://hw1287789687.iteye.com/blog/2188480