2011-10-13 84 views
1

我正在使用Primefaces fileDownload。當我第一次啓動該文件被下載的應用程序,但然後每次我按下下載按鈕,出現此錯誤:Primefaces fileDownload - getOutputStream()已被調用此響應

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; 
     } 

回答

3

您正在bean的構造函數中創建流,而不是在與<p:commandButton>關聯的操作方法中創建流。症狀表明該bean被放置在比請求範圍更廣的範圍內。構造函數只在bean的構造中調用,而不是在每個HTTP請求上調用。如果bean被放入請求範圍,那麼將在每個HTTP請求上調用構造函數。

你有2種選擇:

  1. 把豆在要求範圍之內。

  2. 改爲在操作方法中創建流並將其綁定到<p:commandButton action>

+0

你是一個保佑! – spauny

+0

請你能舉出第二個解決方案的例子。 – Sarz

0

頁面中是否有多個<p:filedownload/>標籤(可能具有相同的綁定)?當嘗試在同一頁面中使用多個啓用了ajax的<p:filedownload/>標籤(綁定到支持bean的不同屬性)以及其他ajax-y功能時,我遇到了Primefaces問題。主要問題似乎是每個<p:filedownload/>都被綁定到相同的屬性。我的項目需求以一種無需使用ajax下載的方式進行了更改,所以我沒有一個好的解決方案,但這可能會幫助您。

+0

不,我沒有多個p:fileDownload,只有一個p:fileDownload和一個p:fileUpload – spauny

相關問題