简体   繁体   中英

JSP : getOutputStream() has already been called for this response

I am using this code to download file from FTP. I am getting exception in tomcat log as below, i am able to perform my task but my log size increases a lot.

Code:

   if (file.exists())
   {
       if (file.canRead())
       {
          // IE6 & SSL PDF Bug
          // http://forums.sun.com/thread.jspa?threadID=526451&start=15&tstart=0

           mimeType = new javax.activation.MimetypesFileTypeMap().getContentType(file);
           response.setHeader("Cache-Control","private");
           response.setHeader("Pragma","expires");
           response.setHeader("Content-Disposition", "inline; filename=\"" + org.apache.commons.io.FilenameUtils.getName(file.getAbsolutePath()) + "\"");
           response.setContentType(mimeType);
           response.setContentLength((new Long(file.length())).intValue());
           byte[] buffer = new byte[(int)org.apache.commons.io.FileUtils.ONE_KB * 64];
           output=response.getOutputStream();
           bos = new java.io.BufferedOutputStream(output, buffer.length);
           bis = new java.io.BufferedInputStream(new java.io.FileInputStream(file));
           while (bis.read(buffer) != -1)
           {
               bos.write(buffer);
           }
           bos.flush();
       }
       else{System.out.println("Cannot read from file");}
   }
   else{System.out.println("File dosen't exist");}

Error Message

    Jan 18, 2014 6:11:31 AM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet jsp threw exception
 java.lang.IllegalStateException: getOutputStream() has already been called for this response
at org.apache.catalina.connector.Response.getWriter(Response.java:611)
at org.apache.catalina.connector.ResponseFacade.getWriter(ResponseFacade.java:198)
at org.apache.jasper.runtime.JspWriterImpl.initOut(JspWriterImpl.java:125)
at org.apache.jasper.runtime.JspWriterImpl.flushBuffer(JspWriterImpl.java:118)
at org.apache.jasper.runtime.PageContextImpl.release(PageContextImpl.java:188)
at org.apache.jasper.runtime.JspFactoryImpl.internalReleasePageContext(JspFactoryImpl.java:118)
at org.apache.jasper.runtime.JspFactoryImpl.releasePageContext(JspFactoryImpl.java:77)

I saw post regarding adding code to servlet, also closing the output stream but none of that worked. Please assist.

you shouldn't do this in JSP but you should use a Servlet (even if a jsp page is far more pratical)

however if you still want to use a jsp page, use this directive:

<%@page language="java" trimDirectiveWhitespaces="true"%>

apart from question, since you are using commons-io:

if (file.exists())
{
    if (file.canRead())
    {
       // IE6 & SSL PDF Bug
       // http://forums.sun.com/thread.jspa?threadID=526451&start=15&tstart=0

        mimeType = new javax.activation.MimetypesFileTypeMap().getContentType(file);
        response.setHeader("Cache-Control","private");
        response.setHeader("Pragma","expires");
        response.setHeader("Content-Disposition", "inline; filename=\"" + org.apache.commons.io.FilenameUtils.getName(file.getAbsolutePath()) + "\"");
        response.setContentType(mimeType);
        response.setHeader("Content-Length", String.valueOf(file.length()));

        OutputStream output = response.getOutputStream();
        FileUtils.copyFile(file, output);
        output.close();
    }
    else{System.out.println("Cannot read from file");}
}
else{System.out.println("File dosen't exist");}

Use a Servlet to send binary data in a response, not a JSP page. JSP pages send text output, and will invoke the getWriter method in order to send the buffered output from the JSP page.

See also this question.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM