4
votes

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.

1
Could you post the whole source code of your xagent. Thanks in advance!Georg Kastenhofer

1 Answers

6
votes

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