码迷,mamicode.com
首页 > 其他好文 > 详细

文件下载

时间:2015-02-05 13:45:47      阅读:182      评论:0      收藏:0      [点我收藏+]

标签:

@RequestMapping(value = "/downloadtradeinfo")
public void downloadtradeinfo(Model model, HttpServletRequest request,HttpServletResponse response) {		
	try {
		String modlename="文件FOO.doc";
		String templateFilePath =  "D:"+File.separator+modlename;
		BufferedInputStream bis=null;
		BufferedOutputStream bos=null;
		OutputStream fos=null;
		InputStream fis = null;
		File file = new File(templateFilePath);
		fis= new FileInputStream(file);
		bis = new BufferedInputStream(fis);

		fos= response.getOutputStream();
		bos= new BufferedOutputStream(fos);
		response.setHeader("Content-disposition", "attachment;filename="+java.net.URLEncoder.encode(modlename, "UTF-8"));
		int bytesRead=0;
		byte[] buffer= new byte[8192];
		while((bytesRead=bis.read(buffer,0,8192))!=-1){
			bos.write(buffer,0,bytesRead);
		}
		bos.flush();
		bis.close();
		bos.close();
		fos.close();
		fis.close();
	} catch (Exception e) {
		e.printStackTrace();
	}	
}

文件下载

标签:

原文地址:http://blog.csdn.net/u010081710/article/details/43525493

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!