Serious: Servlet.service () for Servlet Dispatcherservlet threw exception java.lang.IllegalStateException:

Source: Internet
Author: User
Serious: Servlet.service () for Servlet Dispatcherservlet threw exception

Java.lang.IllegalStateException:getOutputStream () has already been called for this response

At Org.apache.catalina.connector.Response.getWriter (response.java:610)
At Org.apache.catalina.connector.ResponseFacade.getWriter (responsefacade.java:198)


This problem troubled me for a long time did not solve, recently this project I met again, determined to be resolved, this anomaly because Response.getoutputstream () and Response.getwriter () conflict caused, It is also possible that the last information used was not cleared, so the cache needs to be reset. Add response. Resetbuffer () before use;
Now record, send out to share with everyone, hope to help meet the same problem friends, with the code:


response;
"UTF-8 ";      
     =  1024;
= Logfactory.getlog (getclass ())      ;

/** * Output stream to client * *@paramInputStreamInput stream *@paramdestFileNameSave Name *@throwsJava.io.IOException *Public voidWrite (InputStream InputStream, String destfilename)throwsIOException { This.Response. Resetbuffer ()//Reset output stream, empty last left message, prevent error This.Response. setContentType ("Application/octet-stream"); This.Response. setcharacterencoding ( This.encoding); String downLoad ="";if(destFileName!=NULL) {downLoad =NewString (Destfilename.getbytes ("GBK"),"Iso-8859-1"); } This.Response. SetHeader ("Content-disposition","Attachment;filename=\"" + DownLoad +"\""); OutputStream output =NULL;Try{byte[] bytes =New byte[blockSize]; Output = This.Response. Getoutputstream ();intByteread; while((byteread = inputstream.read (bytes))!=-1)
        {output.write (bytes, 0, byteread);
    } output.flush (); }Catch(Exception ex) {if( This.Response. iscommitted ()) {Logger. Error ("Exception class:"+ Ex.getcause (). GetClass (). GetName () +"\ nException message: "+ Ex.getcause (). GetMessage ()); }throw NewIOException (Ex.getmessage ()); }finally{if(InputStream!=NULL){Try{Inputstream.close ();}Catch(Exception e) {Logger. Error (E.getmessage ()); }
        }if(Output!=NULL) {Try{Output.close (); }Catch(Exception e) {Logger. Error (E.getmessage ()); }
        }Response. Flushbuffer (); }
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.