码迷,mamicode.com
首页 > 编程语言 > 详细

java io 文件下载

时间:2017-08-28 00:48:41      阅读:221      评论:0      收藏:0      [点我收藏+]

标签:ram   下载   文件下载   div   oid   let   while   buffere   final   

/**
	 *  文件下载
	 * @param response
	 * @param downloadPath
	 * @param docName
	 */
	public void downLoadFile(
			HttpServletResponse response,String downloadPath, String docName) {
		response.setContentType("text/html;charset=utf-8");
	
		BufferedInputStream bis = null;
		BufferedOutputStream bos = null;

		
		File file = new File(downloadPath);

		try {
			response.addHeader("Content-Length", String.valueOf(file.length()));
			byte[] buff = new byte[2048];
			bis = new BufferedInputStream(new FileInputStream(file));
			bos = new BufferedOutputStream(response.getOutputStream());
			int bytesRead;
			while ((bytesRead = bis.read(buff, 0, buff.length)) != -1) {
				bos.write(buff, 0, buff.length);
			}

		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				if(bis !=null){
					bis.close();
				}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			try {
				if(bos !=null){
					bos.close();
				}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		}
	}
}

  

java io 文件下载

标签:ram   下载   文件下载   div   oid   let   while   buffere   final   

原文地址:http://www.cnblogs.com/newlangwen/p/7440888.html

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