文件下载

来源:互联网 发布:淘宝网有企业账号注册 编辑:程序博客网 时间:2024/05/07 22:14
 <%@ page contentType="text/html;charset=utf-8" pageEncoding="utf-8"%>
<%@ page errorPage="../common/error.jsp"%>
<jsp:directive.page import="java.io.BufferedInputStream"/>
<jsp:directive.page import="java.io.FileInputStream"/>
<jsp:directive.page import="java.io.BufferedOutputStream"/>
<%@ include file="../common/common.jsp"%>
<jsp:directive.page import="com.winekee.common.util.StringHelper"/>
<jsp:directive.page import="com.winekee.common.db.domain.Main"/>
<jsp:directive.page import="java.util.Map"/> 
<%
 //下载内部共享信息的附件
  String id=StringHelper.nvl(request.getParameter("id"));//信息ID
  Map forummap=(Map)sqlMapper.queryForObject("selectBySql","select filepath from forum where id="+id);//查询出文件保存路径
 try
 {
  response.setContentType("application/x-msdownload;"); 
  String  filepath=Main.homepath+ "/upload/forum/"+StringHelper.nvl(forummap.get("filepath")); //文件完整路径
  //获取文件名
  String filename=StringHelper.nvl(forummap.get("filepath")).substring(StringHelper.nvl(forummap.get("filepath")).lastIndexOf("/")+1,StringHelper.nvl(forummap.get("filepath")).length());
  response.setHeader("Content-disposition","attachment; filename="+new String( filename.getBytes("gb2312"),"iso8859-1"));  
  BufferedInputStream bis=new BufferedInputStream(new FileInputStream(filepath));  
  BufferedOutputStream bos=new BufferedOutputStream(response.getOutputStream());  
  //新建缓冲区
  byte[] buff=new byte[2048];  
  int bytesRead;  
  while((bytesRead=bis.read(buff, 0, buff.length))!=-1)  
  {  
   bos.write(buff);  
  }  
  if (bis != null)  
   bis.close();  
  if (bos != null)  
     bos.close();    
 }catch(Exception e)
 {
  e.printStackTrace();
  StringHelper.stringEquals("","系统找不到指定的路径");
 }finally
 {
  out.clear();
  out = pageContext.pushBody();
 }
 %>
原创粉丝点击