Caused by: java.lang.IllegalStateException: Cannot obtain Writer because OutputStream is already in use

1.3k Views Asked by At

I am getting the below error when trying to download file in liferay

05:03:35,867 ERROR [[ACTIVE] ExecuteThread: '0' for queue: 'weblogic.kernel.Default (self-tuning)'][PortletRequestDispatcherImpl:115] org.springframework.web.util.NestedServletException: View rendering failed; nested exception is java.lang.IllegalStateException: Cannot obtain Writer because OutputStream is already in use
org.springframework.web.util.NestedServletException: View rendering failed; nested exception is java.lang.IllegalStateException: Cannot obtain Writer because OutputStream is already in use
        at org.springframework.web.servlet.ViewRendererServlet.processRequest(ViewRendererServlet.java:96)
        at org.springframework.web.servlet.ViewRendererServlet.doGet(ViewRendererServlet.java:67)



Caused by: java.lang.IllegalStateException: Cannot obtain Writer because OutputStream is already in use
        at com.liferay.portlet.MimeResponseImpl.getWriter(MimeResponseImpl.java:90)
        at com.liferay.portlet.PortletServletResponse.getWriter(PortletServletResponse.java:207)
        at com.netcracker.portal.framework.spring.templates.view.SoyDataView.renderMergedOutputModel(SoyDataView.java:93)

and below is the code, i tried searching online but none of them have worked

 String defaultFileName = "hello.pdf";
            resourceResponse.setContentType("application/octet-stream");
            resourceResponse.addProperty(GenericServiceTransport.CONTENT_DISPOSITION, "attachment; filename= hello.pdf" );
            resourceResponse.flushBuffer();

            try {
                out = resourceResponse.getPortletOutputStream();
                out.write(backendFile.getContent());
                out.flush();
                out.close();
            } catch (IOException e) {
                logger.error(e.getMessage());
            }
            return;
1

There are 1 best solutions below

0
Olaf Kock On

As first item I'd try to remove the flushBuffer call. You probably get the exception in the line following it, and flushing the buffer surely will use the output stream.

If that doesn't help, please add mute code, e.g. the complete method including its signature