Fórumok

Error while downloading application/excel mime type

sraddhanjali Jena, módosítva 11 év-val korábban

Error while downloading application/excel mime type

New Member Bejegyzések: 3 Csatlakozás dátuma: 2012.05.21. Legújabb bejegyzések
Hello,
I developed a custom portlet for Liferay 6 where Clicking on a link should download an excel file.
I use response.setContentType ("application/excel").
Below is the code snippet:

ServletOutputStream outs = null;
File f = new File ("/Users/Sraddhanjali/desktop/sony_bmg.xlsx");
response.reset();
response.setContentType ("application/excel");
response.flushBuffer();
outs = response.getOutputStream();

But I am getting the following error:

java.lang.IllegalStateException: Cannot obtain OutputStream because Writer is already in use
at com.liferay.portlet.MimeResponseImpl.getPortletOutputStream(MimeResponseImpl.java:67)
at com.liferay.portlet.PortletServletResponse.getOutputStream(PortletServletResponse.java:188)

Do you have suggestions?

Thanks in advance,
Sraddha
thumbnail
Subhasis Roy, módosítva 11 év-val korábban

RE: Error while downloading application/excel mime type

Expert Bejegyzések: 275 Csatlakozás dátuma: 2012.01.20. Legújabb bejegyzések
I have tried with the following code which worked for me:


HttpServletResponse response = PortalUtil.getHttpServletResponse(res);
response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
res.setContentType("application/vnd.ms-excel");
res.addProperty(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=" + fileName);

try {
OutputStream out = res.getPortletOutputStream();
InputStream in = new FileInputStream(new File(excelPath));
if (in == null) {
out.close();
} else {
byte[] buffer = new byte[4096];
int len;

while ((len = in.read(buffer)) != -1) {
out.write(buffer, 0, len);
}

out.flush();
in.close();
out.close();
}

} catch (Exception ex) {

_log.error(ex);
}
sraddhanjali Jena, módosítva 11 év-val korábban

RE: Error while downloading application/excel mime type

New Member Bejegyzések: 3 Csatlakozás dátuma: 2012.05.21. Legújabb bejegyzések
Hey my question may be silly but I am doing this for the first time.

What is res in the below line ?
HttpServletResponse response = PortalUtil.getHttpServletResponse(res);

And do we need to set the content type two times using response and res ?
response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
res.setContentType("application/vnd.ms-excel");

BTW Thanks !!!
thumbnail
Subhasis Roy, módosítva 11 év-val korábban

RE: Error while downloading application/excel mime type

Expert Bejegyzések: 275 Csatlakozás dátuma: 2012.01.20. Legújabb bejegyzések
"res" is RenderResponse.

public ActionForward render(ActionMapping mapping, ActionForm form,
PortletConfig config, RenderRequest req, RenderResponse res)
throws Exception {


HttpServletResponse response = PortalUtil
.getHttpServletResponse(res);
response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-Disposition",
"attachment;filename=" + fileName);
res.setContentType("application/vnd.ms-excel");
res.addProperty(HttpHeaders.CONTENT_DISPOSITION,
"attachment; filename=" + fileName);
}

I set the content type in both RenderResponse and HttpServletResponse and it worked for me
interesting facts, módosítva 11 év-val korábban

RE: Error while downloading application/excel mime type

Junior Member Bejegyzések: 53 Csatlakozás dátuma: 2012.05.18. Legújabb bejegyzések
Working on it