java - 如何下载Primefaces过滤数据表中的文件?

标签 java jsf primefaces

我有一个数据表,可以让您下载文件。有用。但是,当我尝试下载过滤后的文件时,返回的文件位于列表中的第一个。

此图像显示,当单击 teste4.txt 时,会下载相同的文件。这是对的。 Correct download

另一张图显示,当点击文件teste4.txt时,经过过滤,下载的文件是teste1.txt Download wrong

这是我包含数据表的文件:



    

        

            

                
                

            

        


这是backingBean负责下载的方法:

public void download() 抛出异常 {

    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) 抛出 IOException {

   //美国逻辑信息:http://stackoverflow.com/questions/1686821/execute-backing-bean-action-on-load
    文件描述Arquivo = new File(nomeArquivo);
    整数长度=0;
    OutputStream os = externalContext.getResponseOutputStream();
    字符串 extencaoArquivo = externalContext.getMimeType(nomeArquivo);
    externalContext.setResponseContentType((extencaoArquivo != null) ? extencaoArquivo : "application/octet-stream");
    externalContext.setResponseContentLength((int) descritoArquivo.length());
    externalContext.setResponseHeader("Content-Disposition", "附件; 文件名=\""+ nomeDeExibicao + "\"");

   //流向请求者。
    字节[] bbuf = 新字节[1024];
    DataInputStream in = new DataInputStream(new FileInputStream(descritoArquivo));
    while ((in != null) && ((length = in.read(bbuf)) != -1)) {
        os.write(bbuf, 0, 长度);
    }

    附寄();

}

最佳答案

如果我是你,我会做一些更改以通过方法参数获取当前的 arquivo :

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

在 bean 中:

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

关于java - 如何下载Primefaces过滤数据表中的文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15794679/

相关文章:

jsf - 使用 JSF 生成 HTTP 错误代码页面以及异常错误页面?

java - 用户界面 :include using FaceletsContext

java - java赋值中的魔数(Magic Number)问题

java - 哪种 Camel 构造适合转换?

jsf - 如何停止与 Primefaces 轮询组件的 ajax 通信

jsf - 是否可以创建具有两个目标的选择列表?

jsf-2 - 如何获取 Primefaces 惰性数据表上的过滤值?

java - 如何使用 At4J 或 7-Zip-JBinding 获取文件的 InputStream?

java - 可以使用复合键制作 spring-data-elasticsearch @Document 吗?

java - 关于 JSF 2.0 自定义组件和 Primefaces 的帮助