首页 > 编程语言 > 详细

java io 文件下载

时间:2017-08-27 17:46:47      阅读:251      评论:0      收藏:0      [点我收藏+]
/**
	 *  文件下载
	 * @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 文件下载

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!