无组件的jsp文件下载

来源:互联网 发布:西门子数控铣床编程 编辑:程序博客网 时间:2024/06/05 17:35

<%@ 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("关闭文件流失败");
     }
    }

%>