日期:2014-05-16 浏览次数:20366 次
<%@ page language="java" pageEncoding="gbk"%><%@ page import="org.apache.commons.logging.Log,org.apache.commons.logging.LogFactory" %><% Log log = LogFactory.getLog(this.getClass()); String name_cn=(String)request.getAttribute("name_cn");///获取文件名称 格式 sss.doc String abstrUrl=(String)request.getAttribute("abstrUrl");//获取文件相对或绝对路径,为空走默认! response.setContentType("application/octet-stream;charset=gbk"); // charset=gb2312 String ffnmae=new String(name_cn.getBytes("gb2312"),"iso8859-1");//格式化 名称 response.setHeader("Content-Disposition","attachment; filename="+ffnmae); java.io.FileInputStream in = null; java.io.BufferedInputStream binpu = null; java.io.BufferedOutputStream bout = null; try{ in = new java.io.FileInputStream(abstrUrl); binpu = new java.io.BufferedInputStream(in); bout = new java.io.BufferedOutputStream(response.getOutputStream()); byte[] b = new byte[1024]; int i = 0; while((i = binpu.read(b,0,b.length)) > 0){ bout.write(b, 0, i); } bout.flush(); //要加以下两句话,否则会报错 out = pageContext.pushBody(); response.flushBuffer(); out.clear(); }catch(Exception e){ log.error("下载错误,错误URL:"+abstrUrl, e); }finally{ if(in != null){ try { in.close(); } catch (java.io.IOException e) { log.error("关闭文件错误", e); } in = null; } if(binpu != null){ try { binpu.close(); } catch (java.io.IOException e) { log.error("关闭输入流错误", e); } binpu = null; } if(bout != null){ try { bout.close(); } catch (java.io.IOException e) { log.error("关闭输出流错误", e); } bout = null; } } %>