JSP流下载

 

http://blog.csdn.net/ecjtuxuan/archive/2008/02/14/2093757.aspx

<%@ page language="java" pageEncoding="utf-8"%>
<%@ page import="java.io.OutputStream,java.io.File,java.io.FileInputStream"%>
<html>
  <head>
 
    <title>JSP流文件下载</title>

    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">   
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">

  </head>
 
  <body>
   <%     
          response.reset();
          OutputStream o =response.getOutputStream();    
          byte   b[]=new   byte[500]; 
          String path = "e:\\mysql.sql";
          //String path = request.getSession().getServletContext().getRealPath("/");
         // if(path.endsWith("\\"))
          //{
           //   path+="upload\\excelfiles\\TransInfo.xls";
          //}
          //else
          //{
            //  path+="upload\\excelfiles\\TransInfo.xls";
          //}
          File fileLoad=new File(path); 
          response.reset(); 
          response.setCharacterEncoding("GBK");
          request.setCharacterEncoding("GBK");
          response.setHeader("Content-disposition","attachment;filename="+"mysql.sql");
          response.setContentType("application/x-download");    
          long   fileLength=fileLoad.length();  
          String   length=String.valueOf(fileLength);  
          response.setHeader("Content_Length",length);  
          FileInputStream   in=new   FileInputStream(fileLoad);  
          int   n=0;  
          while((n=in.read(b))!=-1){
             o.write(b,0,n);  
          } 
          in.close();
          o.close();
        
  %>
  </body>
</html>

你可能感兴趣的:(java,sql,jsp,mysql,cache)