Java – primefaces P: FileDownLoad with data table
•
Java
I have a data table that returns all the files in the folder, and I can download the file using the primefaces P: FileDownLoad resource
It works well, but I can't modify the file when the code is loaded because FileInputStream is oppended If I close FileInputStream during data table loading, P: FileDownLoad does not work
anybody?
(if I uncomment the comment section, FileDownLoad does not work. If I keep it, I can't edit the file through @ l_403_0@)
Java:
public List<StreamedContent> getAnexosQuestionarios() { List<StreamedContent> resultado = new ArrayList<StreamedContent>(); 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:
<p:panel header="Questionários"> <p:dataTable id="dtAnexosQuestionarios" value="#{tecnologiaEmpresaController.anexosQuestionarios}" var="arquivo" widgetVar="dtAnexosQuestionarios" emptyMessage="Nenhum anexo disponível" style="width:50%; border:2 !important; border-color:white !important;"> <p:column headerText="" style="width:20px;"> <h:graphicImage value="../resources/images/#{tecnologiaEmpresaController.getExtensao(arquivo.name)}.png" /> </p:column> <p:column headerText="Arquivo"> <p:commandLink id="downloadLink" value="#{arquivo.name}" ajax="false"> <p:fileDownload value="#{arquivo}" /> </p:commandLink> </p:column> </p:dataTable> </p:panel>
thank you!!
Solution
Because Sabrina Bettini, this problem has been solved
This is my code fix:
Code used to populate the data table:
public List<StreamedContent> getAnexosInformacoes() { List<StreamedContent> resultado = new ArrayList<StreamedContent>(); 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,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; }
Code to open a file using 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:
<p:panel header="Informações"> <p:dataTable id="dtAnexosInformacoes" value="#{tecnologiaEmpresaController.anexosInformacoes}" var="arquivo" widgetVar="dtAnexosInformacoes" emptyMessage="Nenhum anexo disponível" style="width:50%; border:2 !important; border-color:white !important;"> <p:column headerText="" style="width:20px;"> <h:graphicImage value="../resources/images/#{tecnologiaEmpresaController.getExtensao(arquivo.name)}.png" /> </p:column> <p:column headerText="Arquivo"> <p:commandLink id="downloadLink" value="#{arquivo.name}" ajax="false" actionListener="#{tecnologiaEmpresaController.prepararArquivoInformacoes(arquivo)}"> <p:fileDownload value="#{tecnologiaEmpresaController.arquivo}" /> </p:commandLink> </p:column> </p:dataTable> </p:panel> </p:panel>
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(); }
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码