Forums

Home » Liferay Portal » English » 3. Development

Combination View Flat View Tree View
Threads [ Previous | Next ]
toggle
Rahul Saraswat
Exception in getting OutputStream from response object
March 13, 2013 8:58 AM
Answer

Rahul Saraswat

Rank: Junior Member

Posts: 60

Join Date: February 26, 2013

Recent Posts

HI Everyone,

I am getting an error in getting the OutputStream from the renderRequest object. The error is as follows :

org.apache.catalina.core.ApplicationDispatcher invoke
SEVERE: Servlet.service() for servlet jsp threw exception
java.lang.IllegalStateException: Cannot obtain Writer because OutputStream is already in use
at com.liferay.portlet.MimeResponseImpl.getWriter(MimeResponseImpl.java:82)



The code snippet is as follows :

HttpServletResponse httpResponse = PortalUtil.getHttpServletResponse(renderResponse);
httpResponse.flushBuffer();
try{
this.outputStream = renderResponse.getPortletOutputStream();
}catch(IOException ioException){
System.out.println(" ");
this.outputStream.close();
this.outputStream.flush();
}


Thanks in advance.

Thanks & Regards
Abhimanyu Saraswat
David H Nebinger
RE: Exception in getting OutputStream from response object
March 13, 2013 9:30 AM
Answer

David H Nebinger

Community Moderator

Rank: Liferay Legend

Posts: 11793

Join Date: September 1, 2006

Recent Posts

You cannot manually take over the output stream. You are not in a servlet anymore.
Rahul Saraswat
RE: Exception in getting OutputStream from response object
March 13, 2013 11:34 AM
Answer

Rahul Saraswat

Rank: Junior Member

Posts: 60

Join Date: February 26, 2013

Recent Posts

Thanks for your reply David. Can you please suggest some way to get the OutputStream.

Thanks & Regards
Rahul Saraswat
David H Nebinger
RE: Exception in getting OutputStream from response object
March 13, 2013 12:02 PM
Answer

David H Nebinger

Community Moderator

Rank: Liferay Legend

Posts: 11793

Join Date: September 1, 2006

Recent Posts

You cannot.

I'm guessing you're trying to pass back an image or some other binary file, but the render response is not the place to do it.
Rahul Saraswat
RE: Exception in getting OutputStream from response object
March 14, 2013 2:19 AM
Answer

Rahul Saraswat

Rank: Junior Member

Posts: 60

Join Date: February 26, 2013

Recent Posts

Thanks for the reply David.
I am trying to create pdf so need to get the OutputStream from the RenderResponse. I 'll use this binary stream in processAction() of the portlet. Kindly suggest.

Thanks & Regards
Abhimanyu Saraswat
David H Nebinger
RE: Exception in getting OutputStream from response object
March 14, 2013 5:40 AM
Answer

David H Nebinger

Community Moderator

Rank: Liferay Legend

Posts: 11793

Join Date: September 1, 2006

Recent Posts

Nope, you cannot do this.

You're not in a servlet world anymore, Abihimanyu. This is not how you accomplish this in the portlet world.

In a portlet world, you use a resource request. Your resource request handler would generate the pdf content and can return it in the portlet's stream.
Rahul Saraswat
RE: Exception in getting OutputStream from response object
March 15, 2013 1:13 AM
Answer

Rahul Saraswat

Rank: Junior Member

Posts: 60

Join Date: February 26, 2013

Recent Posts

Thanks a lot David. It is working fine now after using resourceURL.
Thanks for your time.

Thanks & Regards
Rahul Saraswat
AnandaPadmanaban S
RE: Exception in getting OutputStream from response object
September 16, 2014 6:31 AM
Answer

AnandaPadmanaban S

Rank: New Member

Posts: 1

Join Date: September 16, 2014

Recent Posts

Hello Rahul,

I got stuck with using the resourceURL for downloading the Excel file

FileInputStream file=new FileInputStream(new File("C:\\update.xls"));
try {
// TODO Auto-generated method stub
HttpServletResponse response = PortalUtil.getHttpServletResponse(aResourceResponse);
response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-Disposition", "attachment;filename=" + file);
aResourceResponse.setContentType("application/vnd.ms-excel");
aResourceResponse.addProperty(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=" + file);


fileOut= aResourceResponse.getPortletOutputStream();

When i hit this line i am getting the java.lang.illegalstateException so plz guide me to solve this....
wb.write(fileOut);
Manali Lalaji
RE: Exception in getting OutputStream from response object
September 16, 2014 9:47 AM
Answer

Manali Lalaji

Rank: Expert

Posts: 362

Join Date: March 9, 2010

Recent Posts

Hi,

Directly use ServeResource Request/Response to set content type and headers. No need to use HttpServletResponse.

HTH!
Rahul Saraswat
RE: Exception in getting OutputStream from response object
March 18, 2015 8:47 PM
Answer

Rahul Saraswat

Rank: Junior Member

Posts: 60

Join Date: February 26, 2013

Recent Posts

AnandaPadmanaban S:
Hello Rahul,

I got stuck with using the resourceURL for downloading the Excel file

FileInputStream file=new FileInputStream(new File("C:\\update.xls"));
try {
// TODO Auto-generated method stub
HttpServletResponse response = PortalUtil.getHttpServletResponse(aResourceResponse);
response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-Disposition", "attachment;filename=" + file);
aResourceResponse.setContentType("application/vnd.ms-excel");
aResourceResponse.addProperty(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=" + file);


fileOut= aResourceResponse.getPortletOutputStream();

When i hit this line i am getting the java.lang.illegalstateException so plz guide me to solve this....
wb.write(fileOut);


Hi Ananda,

Try below code :

@ResourceMapping
public void generateFile(final ResourceRequest resourceRequest, final ResourceResponse resourceResponse)
throws PortletException, IOException {
final OutputStream pdfOutputStream = resourceResponse.getPortletOutputStream();
ByteArrayOutputStream pdfByteStream = null;
try {
pdfByteStream = ;//Some byte stream.
final Srting fileName = "test";//Some file name.
resourceResponse.setContentType("application/pdf");
resourceResponse.addProperty("Content-Disposition", "attachment; filename=\"" + fileName + ".pdf\"");
pdfOutputStream.write(pdfByteStream.toByteArray());
} catch (DocumentException e) {
logger.error("Exception caught while generating file." + e);
} catch (SQLException e) {
logger.error("Exception caught while generating file." + e);
} catch (ParserConfigurationException e) {
logger.error("Exception caught while generating file." + e);
} catch (SAXException e) {
logger.error("Exception caught while generating file." + e);
} catch (JDOMException e) {
logger.error("Exception caught while generating file." + e);
} catch (WriterException e) {
logger.error("Exception caught while generating file." + e);
} finally {
FileHelper.closeStream(pdfOutputStream);
FileHelper.closeStream(pdfByteStream);
}
}

Thanks
Rahul Saraswat