天天看點

spring MVC 下載下傳檔案

springle mvc中如何下載下傳檔案呢?

比struts2 下載下傳檔案簡單得多

先看例子:

spring MVC 下載下傳檔案

@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

spring MVC 下載下傳檔案

注意:

(1)使用注解@responsebody 表示action傳回的内容直接作為應答體傳回;

(2)通過produces 設定傳回的content type;

(3)直接傳回要下載下傳的檔案的二進制數組

參考:http://hw1287789687.iteye.com/blog/2188480