Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: IllegalStateException: getOutputStream() has already been called for this response

  1. #1
    Junior Member
    Join Date
    May 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default IllegalStateException: getOutputStream() has already been called for this response

    Hi All,
    In Work Detail Module, 3 sub modules are--> 1.create, 2.update, 3.correct.
    In CREATE Work Detail page, i ll add data and upload photos of work details in createworkdetail.jsp. photos ll stored in D:\CMSMTDP_WorkDetailImages\Anekal\n1.jpeg
    In updateworkdetail.jsp & correctworkdetail.jsp(see attached screen pics) if i click on image it ll show error(refer getOutputStream already response.txt) but image is visible n pop in new window.

    code for downloaded file(D:\CMSMTDP_WorkDetailImages\Anekal) & to view in jsp

    public static void downloadFile(String fileName,HttpServletResponse res) throws WebException
    {
    ServletOutputStream outStream = null;

    int bytesRead=0;
    byte [] byteArray = new byte[4096];
    System.out.println("filename===>"+fileName);
    if(null != fileName)
    {
    try
    {
    System.out.println("inside prior to execution download");
    FileInputStream is=new FileInputStream(new File(fileName));
    res.setContentType("image/jpeg");
    outStream = res.getOutputStream();
    bytesRead = is.read(byteArray);
    while (bytesRead!= -1)
    {
    outStream.write(byteArray, 0, bytesRead);
    bytesRead = is.read(byteArray);
    }

    // Flush and close stream
    outStream.flush();
    outStream.close();
    is.close();
    }
    catch (FileNotFoundException e)
    {
    //throw new WebException(e);
    e.printStackTrace();
    }
    catch (IOException e)
    {
    throw new WebException(e);
    }
    }

    }
    error getting in console:
    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(R esponse.java:610)
    at org.apache.catalina.connector.ResponseFacade.getWr iter(ResponseFacade.java:198)
    at javax.servlet.ServletResponseWrapper.getWriter(Ser vletResponseWrapper.java:112)
    at org.apache.jasper.runtime.JspWriterImpl.initOut(Js pWriterImpl.java:125)
    at org.apache.jasper.runtime.JspWriterImpl.flushBuffe r(JspWriterImpl.java:118)
    at org.apache.jasper.runtime.JspWriterImpl.flush(JspW riterImpl.java:173)
    at org.apache.struts.tiles.taglib.InsertTag$InsertHan dler.doEndTag(InsertTag.java:893)
    at org.apache.struts.tiles.taglib.InsertTag.doEndTag( InsertTag.java:465)
    at org.apache.jsp.jsp.Common.CommonLayout_jsp._jspx_m eth_tiles_005finsert_005f0(CommonLayout_jsp.java:1 88)
    at org.apache.jsp.jsp.Common.CommonLayout_jsp._jspSer vice(CommonLayout_jsp.java:145)
    at org.apache.jasper.runtime.HttpJspBase.service(Http JspBase.java:70)
    at javax.servlet.http.HttpServlet.service(HttpServlet .java:717)
    at org.apache.jasper.servlet.JspServletWrapper.servic e(JspServletWrapper.java:374)
    at org.apache.jasper.servlet.JspServlet.serviceJspFil e(JspServlet.java:342)
    at org.apache.jasper.servlet.JspServlet.service(JspSe rvlet.java:267)
    at javax.servlet.http.HttpServlet.service(HttpServlet .java:717)
    at org.apache.catalina.core.ApplicationFilterChain.in ternalDoFilter(ApplicationFilterChain.java:290)
    at org.apache.catalina.core.ApplicationFilterChain.do Filter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.ApplicationDispatcher.inv oke(ApplicationDispatcher.java:630)
    at org.apache.catalina.core.ApplicationDispatcher.doI nclude(ApplicationDispatcher.java:535)
    at org.apache.catalina.core.ApplicationDispatcher.inc lude(ApplicationDispatcher.java:472)
    at org.apache.struts.action.RequestProcessor.doInclud e(RequestProcessor.java:1110)
    at org.apache.struts.tiles.TilesRequestProcessor.doFo rward(TilesRequestProcessor.java:292)
    at org.apache.struts.tiles.TilesRequestProcessor.proc essTilesDefinition(TilesRequestProcessor.java:271)
    at org.apache.struts.tiles.TilesRequestProcessor.proc essForwardConfig(TilesRequestProcessor.java:332)
    at org.apache.struts.action.RequestProcessor.process( RequestProcessor.java:232)
    at org.apache.struts.action.ActionServlet.process(Act ionServlet.java:1913)
    at org.apache.struts.action.ActionServlet.doGet(Actio nServlet.java:449)
    at javax.servlet.http.HttpServlet.service(HttpServlet .java:617)
    at javax.servlet.http.HttpServlet.service(HttpServlet .java:717)
    at org.apache.catalina.core.ApplicationFilterChain.in ternalDoFilter(ApplicationFilterChain.java:290)
    at org.apache.catalina.core.ApplicationFilterChain.do Filter(ApplicationFilterChain.java:206)
    at in.gov.mrc.kstd.arch.web.WSSORequestFilter.doFilte r(WSSORequestFilter.java:141)
    at org.apache.catalina.core.ApplicationFilterChain.in ternalDoFilter(ApplicationFilterChain.java:235)
    at org.apache.catalina.core.ApplicationFilterChain.do Filter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invo ke(StandardWrapperValve.java:233)
    at org.apache.catalina.core.StandardContextValve.invo ke(StandardContextValve.java:191)
    at org.apache.catalina.core.StandardHostValve.invoke( StandardHostValve.java:128)
    at org.apache.catalina.valves.ErrorReportValve.invoke (ErrorReportValve.java:102)
    at org.apache.catalina.core.StandardEngineValve.invok e(StandardEngineValve.java:109)
    at org.apache.catalina.connector.CoyoteAdapter.servic e(CoyoteAdapter.java:286)
    at org.apache.coyote.http11.Http11Processor.process(H ttp11Processor.java:845)
    at org.apache.coyote.http11.Http11Protocol$Http11Conn ectionHandler.process(Http11Protocol.java:583)
    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run( JIoEndpoint.java:447)
    at java.lang.Thread.run(Thread.java:619)


    please need solutions for this problem. its very urgent..
    thanks in advance..
    Attached Images Attached Images


  2. #2
    Junior Member
    Join Date
    Oct 2011
    Posts
    25
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: IllegalStateException: getOutputStream() has already been called for this respons

    Hi,
    I ll suggest not to use while loop for reading the file data rather create a byte array of file data then at one time write that to response.

    bean factory
    Last edited by abani; December 18th, 2011 at 02:05 AM.

Similar Threads

  1. Replies: 10
    Last Post: July 12th, 2011, 02:27 PM
  2. IllegalStateException getOutputStream
    By NDSPTuba in forum Java Servlet
    Replies: 1
    Last Post: January 15th, 2011, 03:19 PM
  3. Replies: 0
    Last Post: February 22nd, 2010, 01:21 AM
  4. Replies: 0
    Last Post: February 2nd, 2010, 08:20 AM
  5. Replies: 6
    Last Post: April 14th, 2009, 08:02 AM