jsp下载文件方法

java.lang.IllegalStateException: getOutputStream() has already been called for this
写的jsp下载文件是这样的:
所要下载的文档在工程的webroot的pdfDoc文件,从上个网页传递的值是file,
注意:1、url传递中文值时用的是 ISO8859_1格式,所以获取编码后需要转化为utf-8
2、在jsp中使用File类,如果使用tomcat的话,默认的相对路径是tomcat下的bin文件夹,所以需要使用到绝对路径,这里使用 String path1=request. getRealPath(  "/" );获取jsp的绝对路径
<%@   page  language =  "java"   import =  "java.util.*"   pageEncoding  =  "utf-8" %><%@  page   language  = "java"  import =  "java.io.*,java.net.*" %><%
String path = request.getContextPath();
String basePath = request.getScheme()+ "://"  +request.getServerName()+  ":" +request.getServerPort()+path+  "/"  ;
%><%  //获得JSP页面的路径
       String path1=request. getRealPath(  "/" );
           System.out.println(path1);
              response.setContentType(  "text/html"  );
              javax.servlet.ServletOutputStream ou = response.getOutputStream();
              String filepath =  "pdfDoc/"  ;
              String filename =  new  String(request.getParameter( "file"  )
                           .getBytes(  "ISO8859_1"  ),  "utf-8"  ).toString();
              System.out.println(  "DownloadFile filepath:"  + filepath);
              System.out.println(  "DownloadFile filename:"  + filename);
                //java.io.File file1 = new java.io.File("t.txt");
                //System.out.println(java.io.File.separator);
              java.io.File file =  new  java.io.File(path1+filepath + filename);
              
                if  (!file.exists()) {
                     System.out.println(file.getAbsolutePath() +  " 文件不存在!"  );
                       return  ;
              }
                // 读取文件流
              
                // 下载文件
                // 设置响应头和下载保存的文件名
       
                       if  (filename !=  null  && filename.length() > 0) {
                           response.setContentType(  "application/x-msdownload"  );
                           response.setHeader(
                                           "Content-Disposition"  ,
                                           "attachment; filename="
                                                       +  new  String(filename.getBytes( "utf-8"  ),
                                                                       "iso8859-1"  ) +  ""  );
                           java.io.FileInputStream fileInputStream =  null  ;
                           OutputStream output =  null  ;
                             try  {
                                  fileInputStream =  new  java.io.FileInputStream(file);
                                  output = response.getOutputStream();
                                    if  (fileInputStream !=  null ) {
                                           int  filelen = fileInputStream.available();
                                           //文件太大时内存不能一次读出,要循环
                                           byte  a[] =  new  byte [1024];
                                           int  i = 0;
                                           while  ((i = fileInputStream.read(a)) > 0) {
                                                ou.write(a, 0, i);
                                         }
                                         output.flush();
                                  }
                           }  catch  (Exception e) {
                                  System.out.println(  "error"  );
                                  e.printStackTrace();
                           }  finally  {
                                    if  (fileInputStream !=  null ) {
                                         fileInputStream.close();
                                  }
                                    if  (output !=  null ) {
                                         output.close();
                                  }
                           }
                             return  ;
                     }  %> <! DOCTYPE  HTML   PUBLIC   "-//W3C//DTD HTML 4.01 Transitional//EN">
<  html >
   < head  >
  
 
   </ head  >
 
   < body  >
         &nbsp;  </ body  >
</  html >
 
刚开始时能够下载,但是总是异常:java.lang.IllegalStateException: getOutputStream() has already been called for this
后来看了一篇文章说:<%%>和html接触的地方不要有空格说明在如下:(网址是 http://www.360doc.com/content/06/0123/16/677_61900.shtml
 

你可能感兴趣的:(下载文件)