paulwong

springMVC 文件下载

import java.io.File;  
import java.io.IOException;  
  
import org.apache.commons.io.FileUtils;  
import org.springframework.context.annotation.Scope;  
import org.springframework.http.HttpHeaders;  
import org.springframework.http.HttpStatus;  
import org.springframework.http.MediaType;  
import org.springframework.http.ResponseEntity;  
import org.springframework.stereotype.Component;  
import org.springframework.web.bind.annotation.RequestMapping;  
  
/** 
 * <一句话功能简述> 
 * <功能详细描述> 
 *  
 * 
@author  Administrator 
 * 
@version  [版本号, 2014年3月7日] 
 * 
@see  [相关类/方法] 
 * 
@since  [产品/模块版本] 
 
*/  
@Component  
@Scope("prototype")   
@RequestMapping("/downloadFile")  
public class DownloadAction  
{  
  
    @RequestMapping("download")    
    public ResponseEntity<byte[]> download() throws IOException {    
        String path="D:\\workspace\\.metadata\\.plugins\\org.eclipse.wst.server.core\\tmp0\\wtpwebapps\\springMVC\\WEB-INF\\upload\\图片10(定价后).xlsx";  
        File file=new File(path);  
        HttpHeaders headers = new HttpHeaders();    
        String fileName=new String("你好.xlsx".getBytes("UTF-8"),"iso-8859-1");//为了解决中文名称乱码问题  
        headers.setContentDispositionFormData("attachment", fileName);   
        headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);   
        return new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(file),    
                                          headers, HttpStatus.CREATED);    
    }    
}  


JSP
<href="./downloadFile/download" >下载</a>  

posted on 2014-10-29 17:17 paulwong 阅读(2615) 评论(2)  编辑  收藏 所属分类: SPRING MVC

Feedback

# re: springMVC 文件下载[未登录] 2015-06-01 11:34 jimmy

下载大文件的时候会内存溢出  回复  更多评论   

# re: springMVC 文件下载 2015-06-19 10:05 zuidaima

spring mvc demo教程源代码下载:http://zuidaima.com/share/kspringmvc-p1-s1.htm  回复  更多评论   



只有注册用户登录后才能发表评论。


网站导航: