jsp 下载单个文件

<%@page contentType="text/html;charset=GBK"%>
<%@page import="java.io.*,java.util.*"%>

<%

 

OutputStream os=null;
InputStream is = null;
try{
     String FilePath ="";
        String filename ="";
     File fileLoad=new File(FilePath);
     if(!fileLoad.exists())
     {
           %>
      
      <%     
     }
          
        response.reset();
     response.setContentType("application/x-msdownload"); //通知客户文件的MIME类型:
     response.setHeader("Content-disposition","attachment;filename="+filename);
     response.setContentLength((int)fileLoad.length()); //通知客户文件的长度:
     os = response.getOutputStream();
     byte[] buf = new byte[2048]; //输出文件用的字节数组,每次发送2048个字节到输出流:
     int readLength = 0;
     if(fileLoad.exists())
     {
          is = new FileInputStream(fileLoad);
          while ( (readLength = is.read(buf, 0, 2048)) != -1)
          {
               os.write(buf, 0, readLength);
          }
      }
    }catch(Exception ex)
       {
         System.out.println("Error:"+ex.toString());
       }
       finally
       {
         if(os!=null)
         {
           try
           {
             os.close();
           }catch(Exception ex)
           {
             System.out.println("Error:"+ex.toString());
           }
         }
         if(is!=null)
         {
           try
           {
             is.close();
           }catch(Exception ex)
           {
             System.out.println("Error  download:"+ex.toString());
           }
         }

 

%>

你可能感兴趣的:(jsp,exception,javascript,string,download,byte)