java 实现文件下载时,弹出文件保存窗口(IE9)

	public static void downloadLocal(HttpServletRequest request,HttpServletResponse response,
			String fileName,String title,String suffix) 
		throws FileNotFoundException {
		
        title = UtilFunction.convert(title, "gb2312", "ISO8859_1");
		response.reset();
		//告诉浏览器输出内容为流
		response.setContentType("application/octet-stream");
		/*
		 * attachment是以附件下载的形式,inline是以线上浏览的形式。
		 * 当点击“保存”的时候都可以下载,
		 * 当点击“打开”的时候attachment是在本地机里打开,
		 * inline是在浏览器里打开。*/
		response.addHeader("Content-Disposition", "inline;filename=" + title + suffix);
		OutputStream os = null;
		BufferedInputStream bis = null;
		try
		{
			InputStream is = new FileInputStream(fileName);
			bis = new BufferedInputStream(is);
			response.setContentLength(bis.available());
			byte[] buf = new byte[1024];
			int bytes = 0;
			os = response.getOutputStream();
			while ((bytes = bis.read(buf)) != -1)
			{
				os.write(buf, 0, bytes);
			}
		} catch (Exception e)
		{
			log.error(e.getMessage(), e);
		} finally
		{
			try
			{
				if (bis != null)
				{
					bis.close();
				}
				if (os != null)
				{
					os.close();
				}
			} catch (IOException e)
			{
				log.error("down file error", e);
			}
		}
    }

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值