spring mvc - I am getting an exception: java.lang.IllegalStateException: getOutputStream() has already been called for this response -
i want write code download file persisted in system
here code:
in controller class have following mapping
@requestmapping(value = "/processfile", method = requestmethod.post) public @responsebody modelandview downloadfileprocess( @requestparam("file") file originalfile, @requestparam("action") string action, httpservletresponse response) { modelandview model = new modelandview(); model.setviewname("error"); system.out.println(""); system.out.println("action: "+action); model.addobject("message", "action:" + action); try { utility.downloadfile(originalfile, response); message = "the file downloaded successfully"; } catch (ioexception e) { e.printstacktrace(); message = "the process failed due following reason: " + e.getmessage(); } catch (exception e) { e.printstacktrace(); message = "the process failed due following reason: " + e.getmessage(); } model.setviewname("success"); model.addobject("message", message); return model; }
the method in utility class below:
public void downloadfile(file originalfile, httpservletresponse response) throws filenotfoundexception, ioexception { response.setheader("content-disposition", "attachment; filename=" + originalfile.getname()); ioutils.copy( new fileinputstream(new file(string.valueof(originalfile))), response.getoutputstream()); response.flushbuffer(); }
as want download file, have use response.getoutputstream()
.
but getting following exception:
java.lang.illegalstateexception: cannot call sendredirect() after response has been committed @ org.apache.catalina.connector.responsefacade.sendredirect(responsefacade.java:482) @ org.springframework.web.servlet.view.redirectview.sendredirect(redirectview.java:548) @ org.springframework.web.servlet.view.redirectview.rendermergedoutputmodel(redirectview.java:279) @ org.springframework.web.servlet.view.abstractview.render(abstractview.java:267) @ org.springframework.web.servlet.dispatcherservlet.render(dispatcherservlet.java:1221) @ org.springframework.web.servlet.dispatcherservlet.processdispatchresult(dispatcherservlet.java:1005) @ org.springframework.web.servlet.dispatcherservlet.dodispatch(dispatcherservlet.java:952) @ org.springframework.web.servlet.dispatcherservlet.doservice(dispatcherservlet.java:870) @ org.springframework.web.servlet.frameworkservlet.processrequest(frameworkservlet.java:961) @ org.springframework.web.servlet.frameworkservlet.dopost(frameworkservlet.java:863) @ javax.servlet.http.httpservlet.service(httpservlet.java:646) @ org.springframework.web.servlet.frameworkservlet.service(frameworkservlet.java:837) @ javax.servlet.http.httpservlet.service(httpservlet.java:727) @ org.apache.catalina.core.applicationfilterchain.internaldofilter(applicationfilterchain.java:303) @ org.apache.catalina.core.applicationfilterchain.dofilter(applicationfilterchain.java:208) @ org.springframework.web.filter.hiddenhttpmethodfilter.dofilterinternal(hiddenhttpmethodfilter.java:77) @ org.springframework.web.filter.onceperrequestfilter.dofilter(onceperrequestfilter.java:107) @ org.apache.catalina.core.applicationfilterchain.internaldofilter(applicationfilterchain.java:241) @ org.apache.catalina.core.applicationfilterchain.dofilter(applicationfilterchain.java:208) @ org.apache.catalina.core.standardwrappervalve.invoke(standardwrappervalve.java:220) @ org.apache.catalina.core.standardcontextvalve.invoke(standardcontextvalve.java:122) @ org.apache.catalina.authenticator.authenticatorbase.invoke(authenticatorbase.java:503) @ org.apache.catalina.core.standardhostvalve.invoke(standardhostvalve.java:170) @ org.apache.catalina.valves.errorreportvalve.invoke(errorreportvalve.java:103) @ org.apache.catalina.core.standardenginevalve.invoke(standardenginevalve.java:116) @ org.apache.catalina.connector.coyoteadapter.service(coyoteadapter.java:421) @ org.apache.coyote.http11.abstracthttp11processor.process(abstracthttp11processor.java:1070) @ org.apache.coyote.abstractprotocol$abstractconnectionhandler.process(abstractprotocol.java:611) @ org.apache.tomcat.util.net.nioendpoint$socketprocessor.dorun(nioendpoint.java:1736) @ org.apache.tomcat.util.net.nioendpoint$socketprocessor.run(nioendpoint.java:1695) @ java.util.concurrent.threadpoolexecutor.runworker(threadpoolexecutor.java:1142) @ java.util.concurrent.threadpoolexecutor$worker.run(threadpoolexecutor.java:617) @ org.apache.tomcat.util.threads.taskthread$wrappingrunnable.run(taskthread.java:61) @ java.lang.thread.run(thread.java:745) 2015-05-31 12:42:07.547 error 15009 --- [nio-8080-exec-4] o.a.c.c.c.[.[.[/].[dispatcherservlet] : servlet.service() servlet dispatcherservlet threw exception java.lang.illegalstateexception: getoutputstream() has been called response @ org.apache.catalina.connector.response.getwriter(response.java:678) @ org.apache.catalina.connector.responsefacade.getwriter(responsefacade.java:213) @ javax.servlet.servletresponsewrapper.getwriter(servletresponsewrapper.java:104) @ org.springframework.boot.autoconfigure.web.errormvcautoconfiguration$spelview.render(errormvcautoconfiguration.java:187) @ org.springframework.web.servlet.dispatcherservlet.render(dispatcherservlet.java:1221) @ org.springframework.web.servlet.dispatcherservlet.processdispatchresult(dispatcherservlet.java:1005) @ org.springframework.web.servlet.dispatcherservlet.dodispatch(dispatcherservlet.java:952) @ org.springframework.web.servlet.dispatcherservlet.doservice(dispatcherservlet.java:870) @ org.springframework.web.servlet.frameworkservlet.processrequest(frameworkservlet.java:961) @ org.springframework.web.servlet.frameworkservlet.dopost(frameworkservlet.java:863) @ javax.servlet.http.httpservlet.service(httpservlet.java:646) @ org.springframework.web.servlet.frameworkservlet.service(frameworkservlet.java:837) @ javax.servlet.http.httpservlet.service(httpservlet.java:727) @ org.apache.catalina.core.applicationfilterchain.internaldofilter(applicationfilterchain.java:303) @ org.apache.catalina.core.applicationfilterchain.dofilter(applicationfilterchain.java:208) @ org.springframework.web.filter.onceperrequestfilter.dofilter(onceperrequestfilter.java:101) @ org.apache.catalina.core.applicationfilterchain.internaldofilter(applicationfilterchain.java:241) @ org.apache.catalina.core.applicationfilterchain.dofilter(applicationfilterchain.java:208) @ org.apache.catalina.core.applicationdispatcher.invoke(applicationdispatcher.java:748) @ org.apache.catalina.core.applicationdispatcher.doinclude(applicationdispatcher.java:604) @ org.apache.catalina.core.applicationdispatcher.include(applicationdispatcher.java:543) @ org.apache.catalina.core.standardhostvalve.custom(standardhostvalve.java:467) @ org.apache.catalina.core.standardhostvalve.status(standardhostvalve.java:342) @ org.apache.catalina.core.standardhostvalve.throwable(standardhostvalve.java:434) @ org.apache.catalina.core.standardhostvalve.invoke(standardhostvalve.java:205) @ org.apache.catalina.valves.errorreportvalve.invoke(errorreportvalve.java:103) @ org.apache.catalina.core.standardenginevalve.invoke(standardenginevalve.java:116) @ org.apache.catalina.connector.coyoteadapter.service(coyoteadapter.java:421) @ org.apache.coyote.http11.abstracthttp11processor.process(abstracthttp11processor.java:1070) @ org.apache.coyote.abstractprotocol$abstractconnectionhandler.process(abstractprotocol.java:611) @ org.apache.tomcat.util.net.nioendpoint$socketprocessor.dorun(nioendpoint.java:1736) @ org.apache.tomcat.util.net.nioendpoint$socketprocessor.run(nioendpoint.java:1695) @ java.util.concurrent.threadpoolexecutor.runworker(threadpoolexecutor.java:1142) @ java.util.concurrent.threadpoolexecutor$worker.run(threadpoolexecutor.java:617) @ org.apache.tomcat.util.threads.taskthread$wrappingrunnable.run(taskthread.java:61) @ java.lang.thread.run(thread.java:745) 2015-05-31 12:42:07.551 error 15009 --- [nio-8080-exec-4] o.a.c.c.c.[tomcat].[localhost] : exception processing errorpage[errorcode=0, location=/error] org.springframework.web.util.nestedservletexception: request processing failed; nested exception java.lang.illegalstateexception: getoutputstream() has been called response @ org.springframework.web.servlet.frameworkservlet.processrequest(frameworkservlet.java:973) @ org.springframework.web.servlet.frameworkservlet.dopost(frameworkservlet.java:863) @ javax.servlet.http.httpservlet.service(httpservlet.java:646) @ org.springframework.web.servlet.frameworkservlet.service(frameworkservlet.java:837) @ javax.servlet.http.httpservlet.service(httpservlet.java:727) @ org.apache.catalina.core.applicationfilterchain.internaldofilter(applicationfilterchain.java:303) @ org.apache.catalina.core.applicationfilterchain.dofilter(applicationfilterchain.java:208) @ org.springframework.web.filter.onceperrequestfilter.dofilter(onceperrequestfilter.java:101) @ org.apache.catalina.core.applicationfilterchain.internaldofilter(applicationfilterchain.java:241) @ org.apache.catalina.core.applicationfilterchain.dofilter(applicationfilterchain.java:208) @ org.apache.catalina.core.applicationdispatcher.invoke(applicationdispatcher.java:748) @ org.apache.catalina.core.applicationdispatcher.doinclude(applicationdispatcher.java:604) @ org.apache.catalina.core.applicationdispatcher.include(applicationdispatcher.java:543) @ org.apache.catalina.core.standardhostvalve.custom(standardhostvalve.java:467) @ org.apache.catalina.core.standardhostvalve.status(standardhostvalve.java:342) @ org.apache.catalina.core.standardhostvalve.throwable(standardhostvalve.java:434) @ org.apache.catalina.core.standardhostvalve.invoke(standardhostvalve.java:205) @ org.apache.catalina.valves.errorreportvalve.invoke(errorreportvalve.java:103) @ org.apache.catalina.core.standardenginevalve.invoke(standardenginevalve.java:116) @ org.apache.catalina.connector.coyoteadapter.service(coyoteadapter.java:421) @ org.apache.coyote.http11.abstracthttp11processor.process(abstracthttp11processor.java:1070) @ org.apache.coyote.abstractprotocol$abstractconnectionhandler.process(abstractprotocol.java:611) @ org.apache.tomcat.util.net.nioendpoint$socketprocessor.dorun(nioendpoint.java:1736) @ org.apache.tomcat.util.net.nioendpoint$socketprocessor.run(nioendpoint.java:1695) @ java.util.concurrent.threadpoolexecutor.runworker(threadpoolexecutor.java:1142) @ java.util.concurrent.threadpoolexecutor$worker.run(threadpoolexecutor.java:617) @ org.apache.tomcat.util.threads.taskthread$wrappingrunnable.run(taskthread.java:61) @ java.lang.thread.run(thread.java:745) caused by: java.lang.illegalstateexception: getoutputstream() has been called response @ org.apache.catalina.connector.response.getwriter(response.java:678) @ org.apache.catalina.connector.responsefacade.getwriter(responsefacade.java:213) @ javax.servlet.servletresponsewrapper.getwriter(servletresponsewrapper.java:104) @ org.springframework.boot.autoconfigure.web.errormvcautoconfiguration$spelview.render(errormvcautoconfiguration.java:187) @ org.springframework.web.servlet.dispatcherservlet.render(dispatcherservlet.java:1221) @ org.springframework.web.servlet.dispatcherservlet.processdispatchresult(dispatcherservlet.java:1005) @ org.springframework.web.servlet.dispatcherservlet.dodispatch(dispatcherservlet.java:952) @ org.springframework.web.servlet.dispatcherservlet.doservice(dispatcherservlet.java:870) @ org.springframework.web.servlet.frameworkservlet.processrequest(frameworkservlet.java:961) ... 27 common frames omitted
httpservletresponse.getoutputstream()
- used write binary data client.
- data written client not encoded.
- once flush() called, data flushed client.
illegalstateexception
thrown if getwriter() method has been called on response object.
httpservletresponse.getwriter()
- returns printwriter object can used write character text (non-binary) data client.
- data sent client encoded either using default encoding scheme or user provided.
- once flush() called, data flushed client.
illegalstateexception
thrown if getoutputstream() method has been called response object
one key usage difference between 2 getoutputstream() sending binary data client , getwriter() sending encoded text data.
bottom line: either of above 2 methods should used flush data client, , not both, , developer has make sure handled, specially when doing things in non-servlet class.
Comments
Post a Comment