日期:2014-05-18  浏览次数:20626 次

如何实现文件下载功能
在JSP页面中如何来实现文件下载的功能???

------解决方案--------------------
Java code
    
String fileName = "C:\\orderStatistic\\2007年05月23日产品销售统计表.xls";
    
int length = 4096;            
byte [] buffer  =   new   byte [length]; //  缓冲区 
BufferedOutputStream output  =   null ;
BufferedInputStream input  =   null ;
File file = new File(fileName);
         
try{
    response.setContentType("application/octet-stream");
    String downFileName  = new  String(file.getName().getBytes("iso8859-1"),  "gb2312" );
            response.setHeader("Content-Disposition", "attachment;filename=\""+downFileName+"\"");
            
            
            output = new BufferedOutputStream(response.getOutputStream());
            input = new BufferedInputStream(new FileInputStream(file));

            int n = 0;
            while((n = input.read(buffer, 0 , length)) != - 1 ) {
               output.write(buffer, 0, n);
            }
            response.flushBuffer();
        } catch(Exception e) {
            e.printStackTrace();
        }
        finally  {
          try{
              if(input != null ) input.close();
              if(output != null ) output.close();
          } catch(IOException ioe) {
              ioe.printStackTrace();
          }
       }