2013-04-03 38 views
1

我有一個數據表,它允許你下載文件。有用。但是,當我嘗試下載文件後,返回列表中的第一個。如何在Primefaces的過濾數據表中下載文件?

此圖顯示在teste4.txt上單擊時下載了相同的文件。這是對的。 Correct download

這其他的圖片顯示,在文件teste4.txt點擊後,過濾後,下載的文件被teste1.txt Download wrong

這是一個包含數據表我的文件:

<h:form id="hFormListaArquivosRegiao2" enctype="multipart/form-data"> 

    <p:dataTable id="pDataTableListaArquivos" var="arquivo" value="#{arquivoBean.listaArquivos}" filteredValue="#{arquivoBean.filteredListaArquivos}"> 

     <p:column id="pColumnNomeArquivo" headerText="#{msg.NomeDoArquivo}" sortBy="#{arquivo.nomeArquivo}" filterMatchMode="contains" filterBy="#{arquivo.nomeArquivo}"> 

      <h:commandLink action="#{arquivoBean.download}" title="#{arquivo.nomeArquivo}"> 

       <h:outputText value="#{arquivo.nomeArquivo}" /> 
       <f:setPropertyActionListener target="#{arquivoBean.arquivo}" value="#{arquivo}" /> 

      </h:commandLink> 

     </p:column> 

</h:form> 

這是方法backingBean負責下載:

public void download() throws Exception { 

    logger.debug("NOME ARQUIVO: "+ ContextoBean.CAMINHO_ARQUIVOS + arquivo.getNomeArquivo() +", "+ arquivo.getNomeArquivo()); 

    FacesContext facesContext = FacesContext.getCurrentInstance(); 
    pushArquivo(facesContext.getExternalContext(), ContextoBean.CAMINHO_ARQUIVOS + arquivo.getNomeArquivo(), arquivo.getNomeArquivo()); 
    facesContext.responseComplete(); 

} 

這是backingBean的助手方法,負責下載:

private void pushArquivo(ExternalContext externalContext, String nomeArquivo, String nomeDeExibicao) throws IOException { 

    // Info sobre a lógica usada: http://stackoverflow.com/questions/1686821/execute-backing-bean-action-on-load 
    File descritoArquivo = new File(nomeArquivo); 
    int length = 0; 
    OutputStream os = externalContext.getResponseOutputStream(); 
    String extencaoArquivo = externalContext.getMimeType(nomeArquivo); 
    externalContext.setResponseContentType((extencaoArquivo != null) ? extencaoArquivo : "application/octet-stream"); 
    externalContext.setResponseContentLength((int) descritoArquivo.length()); 
    externalContext.setResponseHeader("Content-Disposition", "attachment; filename=\"" + nomeDeExibicao + "\""); 

    // Stream to the requester. 
    byte[] bbuf = new byte[1024]; 
    DataInputStream in = new DataInputStream(new FileInputStream(descritoArquivo)); 
    while ((in != null) && ((length = in.read(bbuf)) != -1)) { 
     os.write(bbuf, 0, length); 
    } 

    in.close(); 

} 

+0

您是否嘗試過使用Primefaces' [下載組件](http://www.primefaces.org/showcase/ui/fileDownload.jsf)?它應該與自己的過濾器兼容 –

+0

是的,我試過了。我實現了這樣的事情:[Primefaces p:fileDownload with datatable](http://stackoverflow.com/questions/14144306/primefaces-pfiledownload-with-datatable) – fenix

回答

0

如果我是你,我會做一些修改,以獲得通過方法參數的當前arquivo:

<h:commandLink action="#{arquivoBean.download(arquivo)}"> 
    <h:outputText value="#{arquivo.nomeArquivo}" /> 
</h:commandLink> 

在豆:

public void download(Arquivo arquivo) throws Exception { 
    FacesContext facesContext = FacesContext.getCurrentInstance(); 
    pushArquivo(facesContext.getExternalContext(), 
     ContextoBean.CAMINHO_ARQUIVOS + arquivo.getNomeArquivo(), 
     arquivo.getNomeArquivo()); 
    facesContext.responseComplete(); 
} 
+0

此解決方案產生了一個異常:WARNING [javax.enterprise.resource .webcontainer.jsf.lifecycle](http-localhost-127.0.0.1-8080-3)#{arquivoBean.download(arquivo)}:java.lang.NullPointerException:javax.faces.FacesException:#{arquivoBean.download(arquivo) }:java.lang.NullPointerException – fenix

+0

你在做什麼JSF? –

+0

我的實現是Mojarra 2.0 – fenix

相關問題