当前位置: 代码迷 >> Java Web开发 >> 怎么实现文件下载功能
  详细解决方案

怎么实现文件下载功能

热度:74   发布时间:2016-04-17 12:20:43.0
如何实现文件下载功能
在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();          }       }
  相关解决方案