现在的位置: 首页 > 综合 > 正文

无组件的jsp文件下载

2013年08月17日 ⁄ 综合 ⁄ 共 1090字 ⁄ 字号 评论关闭

<%@ page language="java" %>
<%@ page session="true" %>
<%@ page isThreadSafe="true" %>
<%@ page contentType="text/html;charset=UTF-8"%>
<%@ page import="java.io.*,java.util.*" %>

 

<%

String fileReallyName = request.getParameter("file");

File zipFile = new File(fileReallyNam);

if(!zipFile.exist()){

   out.print("文件不存在");

 return;

}

      response.setContentType("application/x-msdownload; charset=gb2312");
       response.setHeader("Content-disposition", "attachment;filename=/"" + new String(fileReallyName.getBytes("GBK"),"ISO8859-1") + "/"");  
    FileInputStream fis = null;
    ByteArrayOutputStream bos = null;
    try
    {
     fis = new FileInputStream(zipFile);
     bos = new ByteArrayOutputStream(8192);
     int readline;
     byte[] b = new byte[1024];
     while((readline=fis.read(b))!=-1)
      bos.write(b,0,readline);
     bos.writeTo(response.getOutputStream());
     out.clear();
     out = pageContext.pushBody();
    }catch(IOException e)
    {

           out.print("下载出错"+e.getMessage());
         }finally{
     try{
        if(fis!=null) fis.close();
        if(bos!=null) bos.close();
     }catch(Exception e){
        out.print("关闭文件流失败");
     }
    }

%>

抱歉!评论已关闭.