Primefaces fileDownload - 已为此响应调用 getOutputStream()
我正在使用 Primefaces 文件下载。当我第一次启动应用程序时,会下载文件,但每次按下下载按钮时,都会出现此错误:
java.lang.IllegalStateException: getOutputStream() has already been called for this response
我的 xhtml 代码:
<p:commandButton value="Download" ajax="true">
<p:fileDownload value="#{fileDownloadController.file}" />
</p:commandButton>
我的 java 代码:
private StreamedContent file;
public FileDownloadController() {
InputStream stream = null;
try {
stream = FacesContext.getCurrentInstance().getExternalContext().getResourceAsStream("/Enastr1.txt");
file = new DefaultStreamedContent(stream, "txt", "Downloaded_Enastr1");
} catch (Exception ex) {
Logger.getLogger(FileDownloadController.class.getName()).log(Level.SEVERE, null, ex);
} finally {
}
}
public StreamedContent getFile() {
return file;
}
public void setFile(StreamedContent file) {
this.file = file;
}
I'm using Primefaces fileDownload. When I first start the application the file is downloaded but then everytime I press the download button, this error appears:
java.lang.IllegalStateException: getOutputStream() has already been called for this response
My xhtml code:
<p:commandButton value="Download" ajax="true">
<p:fileDownload value="#{fileDownloadController.file}" />
</p:commandButton>
My java code:
private StreamedContent file;
public FileDownloadController() {
InputStream stream = null;
try {
stream = FacesContext.getCurrentInstance().getExternalContext().getResourceAsStream("/Enastr1.txt");
file = new DefaultStreamedContent(stream, "txt", "Downloaded_Enastr1");
} catch (Exception ex) {
Logger.getLogger(FileDownloadController.class.getName()).log(Level.SEVERE, null, ex);
} finally {
}
}
public StreamedContent getFile() {
return file;
}
public void setFile(StreamedContent file) {
this.file = file;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您在 bean 的构造函数中创建流,而不是在与
关联的操作方法中创建流。症状表明 bean 放置在比请求范围更宽的范围中。构造函数仅在 bean 构造时调用,而不是在每个 HTTP 请求时调用。如果将 bean 放入请求范围内,则每个 HTTP 请求都会调用构造函数。您有 2 个选项:
将 bean 放入请求范围。
改为在操作方法中创建流并将其绑定到
。You're creating the stream in the constructor of the bean instead of in the action method associated with the
<p:commandButton>
. The symptoms indicate that the bean is placed in a broader scope than the request scope. The constructor is only called upon bean's construction, not on every HTTP request. If the bean is put in the request scope, then the constructor is called on every HTTP request.You have 2 options:
Put the bean in the request scope.
Create the stream in an action method instead and bind it to
<p:commandButton action>
.您的页面中是否有多个
标记(可能具有相同的绑定)?当我尝试在同一页面中与其他 ajax-y 功能一起使用多个支持 ajax 的
标记(绑定到支持 bean 中的不同属性)时,我遇到了 Primefaces 的问题。主要问题似乎是每个
都绑定到相同的属性。我的项目的需求发生了变化,消除了对 ajax 下载的需求,因此我没有适合您的解决方案,但这可能会对您有所帮助。Are there multiple
<p:filedownload/>
tags in your page (possibly with the same binding)? I had issues with Primefaces when attempting to use multiple ajax-enabled<p:filedownload/>
tags (bound to different properties in the backing bean) in the same page with other ajax-y functionality. The main problem seemed to be that every<p:filedownload/>
became bound to the same property. My project's requirements changed in a way that removed the need for ajax downloads, so I don't have a good solution for you, but this might help you on your way.