如何从 xPages 流式传输文件? [英] How to stream file from xPages?
问题描述
我正在尝试将文件从文件系统流式传输到浏览器,但无法正常工作.我有一个带有 render=false 的 xpage,在 afterRenderResponse 上我有以下代码:
XspHttpServletResponse response = (XspHttpServletResponse) getFacesContext().getExternalContext().getResponse();response.setContentType("application/octet-stream");response.setHeader("Content-Disposition","attachment;filename=demofile.exe");File file = new File("文件路径");FileInputStream fileIn = new FileInputStream(file);ServletOutputStream out = response.getOutputStream();等等. .....
现在,当我尝试打开 xpage 时,我在控制台上收到错误消息:
java.lang.IllegalStateException:当 Writer 已在使用时无法获取 OutputStream在 com.ibm.xsp.webapp.XspHttpServletResponse.getOutputStream(XspHttpServletResponse.java:548)
'response.getOutputStream()' 方法引发了这样的错误,所以我无法让输出流工作.有没有人有这方面的经验?我只是想实现下载服务,以便我可以将文件从服务器文件系统流式传输回浏览器.
您可以在 beforeRenderResponse 中调用 facesContext.getOutputStream() 而不是从 afterRenderResponse 中调用.
有关更多帮助,请参阅以下链接:
http://www.wissel.net/blog/d6plinks/SHWL-8BYMW8>
http://www.wissel.net/blog/d6plinks/shwl-7mgfbn>
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.
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
这篇关于如何从 xPages 流式传输文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!