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 (); }
}