Question

I'm trying to stream the file from file system to browser and cant get it to work properly. I have a xpage with rendered=false and on afterRenderResponse I have the following code:

XspHttpServletResponse  response = (XspHttpServletResponse) getFacesContext().getExternalContext().getResponse();

response.setContentType("application/octet-stream");
response.setHeader("Content-Disposition","attachment;filename=demofile.exe");

File file = new File("path to file");
FileInputStream fileIn = new FileInputStream(file);
ServletOutputStream out = response.getOutputStream();
etc. .....

Now when I try to open xpage I'm getting the error message as this on console:

java.lang.IllegalStateException: Can't get an OutputStream while a Writer is already in use
       at com.ibm.xsp.webapp.XspHttpServletResponse.getOutputStream(XspHttpServletResponse.java:548)

The method 'response.getOutputStream()' raises such error so I cant get output stream to work. Is there anyone having experience with this? I'm simply trying to implement the download service so I can stream files from server file system back to browser.

Was it helpful?

Solution

You can call the facesContext.getOutputStream() in the beforeRenderResponse and NOT from afterRenderResponse.

Refer to the below links for more help:

http://www.wissel.net/blog/d6plinks/SHWL-8BYMW8

http://www.wissel.net/blog/d6plinks/shwl-7mgfbn

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top