我有一个数据表,可以让您下载文件。有用。但是,当我尝试下载过滤后的文件时,返回的文件位于列表中的第一个。
此图像显示,当单击 teste4.txt 时,会下载相同的文件。这是对的。
另一张图显示,当点击文件teste4.txt时,经过过滤,下载的文件是teste1.txt
这是我包含数据表的文件:
这是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/