ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [JSP] 파일 다운로드 구현 - java.lang.IllegalStateException: getOutputStream()
    Legacy/문제해결 2015. 12. 7. 15:04
    728x90

    파일 다운로드를 구현하다가 다음과 같은 에러가 떴다.

    JSP(서블릿 아님.)에서 getOutputStream()를 호출하는 과정에서 에러가 등장했다.

    파일이 정상적으로 다운로드 되긴 하는데 콘솔화면에는 다음과 같은 에러메시지가 있었다.


    12월 07, 2015 2:50:55 오후 org.apache.catalina.core.StandardWrapperValve invoke

    심각: Servlet.service() for servlet [jsp] in context with path [/Project_WebService] threw exception [java.lang.IllegalStateException: getOutputStream() has already been called for this response] with root cause

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

    at org.apache.catalina.connector.Response.getWriter(Response.java:578)

    at org.apache.catalina.connector.ResponseFacade.getWriter(ResponseFacade.java:212)

    at org.apache.jasper.runtime.JspWriterImpl.initOut(JspWriterImpl.java:115)

    at org.apache.jasper.runtime.JspWriterImpl.flushBuffer(JspWriterImpl.java:108)

    at org.apache.jasper.runtime.PageContextImpl.release(PageContextImpl.java:184)

    at org.apache.jasper.runtime.JspFactoryImpl.internalReleasePageContext(JspFactoryImpl.java:120)

    at org.apache.jasper.runtime.JspFactoryImpl.releasePageContext(JspFactoryImpl.java:75)

    at org.apache.jsp.Tool_005fDownload_jsp._jspService(Tool_005fDownload_jsp.java:230)

    at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)

    at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)

    at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:438)

    at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:396)

    at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:340)

    at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)

    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)

    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)

    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)

    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)

    at org.apache.catalina.filters.CorsFilter.handleNonCORS(CorsFilter.java:436)

    at org.apache.catalina.filters.CorsFilter.doFilter(CorsFilter.java:177)

    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)

    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)

    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:217)

    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)

    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)

    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:142)

    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)

    at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:616)

    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)

    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:518)

    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1091)

    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:673)

    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1500)

    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1456)

    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)

    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)

    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)

    at java.lang.Thread.run(Unknown Source)



    우선 에러가 뜬 것부터 좋지 않은데 심각이라고 하니 조금 무서웠다.

    구글링을 해보니 JSP를 Servlet으로 변환하는 과정에서 생기는 OutputStream인 JspWriter 때문인데 getOutputStream()을 호출하기 전에 JSP 자체의 OutputStream을 제거할 필요가 있다고 한다. 때문에 getOutputStream()을 호출하기 전에 다음과 같은 코드를 삽입하였다.


    out.clear();

    out=pageContext.pushBody();




    참고 - http://sarc.io/index.php/java/276-jsp-java-lang-illegalstateexception-getoutputstream-has-already-been-called-for-this-response


Designed by Tistory.