spring MVC 下载文件(转)

 

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

 

posted @ 2015-03-03 21:34  沧海一滴  阅读(796)  评论(0编辑  收藏  举报