Primefaces p:带数据表的fileDownload

我有一个返回文件夹的所有文件的数据表,并且可以使用primefaces p:filedownload资源下载该文件。

它运行正常,但是当加载代码时我无法修改文件,因为FileInputStream是oppened。 如果我在数据表加载期间关闭FileInputStream,则p:filedownload不起作用

任何人?

(如果我取消注释注释部分,filedownload不起作用,如果我保留它,我不能通过Windows编辑文件)

Java的:

public List getAnexosQuestionarios() { List resultado = new ArrayList(); File[] arquivos = FileHelper.listarArquivos(selected.getEmpresa().getDiretorio(), FileHelper.QUESTIONARIOS); if (arquivos != null) { for (File arquivo : arquivos) { InputStream stream = null; try { stream = new FileInputStream(arquivo.getAbsolutePath()); String extensao = arquivo.getName().substring(arquivo.getName().lastIndexOf(".") + 1); StreamedContent file = new DefaultStreamedContent(stream, MimeTypes.valueOf(extensao).getMimeType(), arquivo.getName()); resultado.add(file); } catch (FileNotFoundException e) { e.printStackTrace(); } } // try { // stream.close(); // } catch (IOException e) { // // TODO Auto-generated catch block // e.printStackTrace(); // } } return resultado; } 

HTML:

 

谢谢 !!

由于sabrina.bettini,这个问题得到了解决

这是我的代码修复:

用于填充数据表的代码:

  public List getAnexosInformacoes() { List resultado = new ArrayList(); File[] arquivos = FileHelper.listarArquivos(selected.getEmpresa().getDiretorio(), FileHelper.INFORMACOES); if (arquivos != null) { for (File arquivo : arquivos) { InputStream stream = null; try { stream = new FileInputStream(arquivo.getAbsolutePath()); String extensao = arquivo.getName().substring(arquivo.getName().lastIndexOf(".") + 1); StreamedContent file = new DefaultStreamedContent(stream,MimeTypes.valueOf(extensao).getMimeType(),arquivo.getName()); resultado.add(file); } catch (FileNotFoundException e) { e.printStackTrace(); } try { stream.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } return resultado; } 

使用actionlistener打开文件的代码:

 StreamedContent arquivo; public void prepararArquivoInformacoes(final StreamedContent arq) { InputStream stream = null; String caminho = FileHelper.retornarCaminhoPasta(selected.getEmpresa().getDiretorio(), FileHelper.INFORMACOES); try { stream = new FileInputStream(caminho + File.separator + arq.getName()); this.arquivo = new DefaultStreamedContent(stream, MimeTypes.valueOf("pdf").getMimeType(), arq.getName()); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public StreamedContent getArquivo() { return arquivo; } 

HTML:

               

FileHelper:

 static FileService fileService; public static final String PASTA_RAIZ = "P:\\"; public static final String INFORMACOES = "1. Informacoes"; public static final String QUESTIONARIOS = "2. Questionarios"; public static final String RELATORIOS = "3_Relatorio"; public static File[] listarArquivos(final String empresa, final String tipo) { File file = new File(PASTA_RAIZ + empresa + File.separator + tipo + File.separator); return file.listFiles(); } public static String retornarCaminhoPasta(final String empresa, final String tipo) { File file = new File(PASTA_RAIZ + empresa + File.separator + tipo + File.separator); return file.getAbsolutePath(); } 

尝试使用StreamedContent file = new DefaultStreamedContent(stream,“application / octet-stream”,arquivo.getName());


这就是我在我的应用程序中的做法:

我不使用数据表。 我使用ui:通过ArquivoAnexo列表重复迭代。

     public void prepararDownloadArquivo(ArquivoAnexo arquivo) { byte[] conteudo = arquivo.getArquivo(); String nome = arquivo.getNomeArquivo(); this.arquivoParaDownload = new DefaultStreamedContent(new ByteArrayInputStream(conteudo), "application/octet-stream", nome); } public StreamedContent getArquivoParaDownload() { return arquivoParaDownload; } public interface ArquivoAnexo { byte[] getArquivo(); String getNomeArquivo(); String getDescricao(); void setDescricao(String descricao); void setArquivo(byte[] conteudo); void setNomeArquivo(String nome); }