Primefaces fileDownload - 已为此响应调用 getOutputStream()

发布于 2024-12-09 16:13:44 字数 1140 浏览 0 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

掀纱窥君容 2024-12-16 16:13:44

您在 bean 的构造函数中创建流,而不是在与 关联的操作方法中创建流。症状表明 bean 放置在比请求范围更宽的范围中。构造函数仅在 bean 构造时调用,而不是在每个 HTTP 请求时调用。如果将 bean 放入请求范围内,则每个 HTTP 请求都会调用构造函数。

您有 2 个选项:

  1. 将 bean 放入请求范围。

  2. 改为在操作方法中创建流并将其绑定到

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:

  1. Put the bean in the request scope.

  2. Create the stream in an action method instead and bind it to <p:commandButton action>.

倾城°AllureLove 2024-12-16 16:13:44

您的页面中是否有多个 标记(可能具有相同的绑定)?当我尝试在同一页面中与其他 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文