程序员一代码搬运工
【Java学习心得和Java学习技术个人总结】

springmvc文件下载

 

@RequestMapping("/download")  
	public void download(HttpServletResponse res ,HttpServletRequest req) throws IOException {  
	    OutputStream os = res.getOutputStream();  
	    try {  
	        res.reset();  
	        res.setHeader("Content-Disposition", "attachment; filename=down.xls");  
	        res.setContentType("application/octet-stream; charset=utf-8");  
	        os.write(FileUtils.readFileToByteArray(getDictionaryFile(req)));  
	        os.flush();  
	    } finally {  
	        if (os != null) {  
	            os.close();  
	        }  
	    }  
	}  
	
	 private File getDictionaryFile(HttpServletRequest req) {
		File file =new File(req.getSession().getServletContext().getRealPath("/WEB-INF/down.xls"));
		return file;
	}

 

 

 

http://www.iteye.com/topic/1125784

赞(0)
未经允许不得转载:程序员一代码搬运工 » springmvc文件下载
分享到: 更多 (0)