I got a datatable that return all files of a folder, and that file can be downloaded using the primefaces p:filedownload resource.
It is working fine, but when the code is loaded i cant modify the file because the FileInputStream is oppened. And if i close the FileInputStream during the datatable load, p:filedownload doesnt work
Anyone?
(If i uncomment the commented part, the filedownload doesnt work, and if i keep it, i cant edit the file trough the windows)
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 !!
The question was solved, thanks to sabrina.bettini
here's my code fixed:
Code to fill the datatable:
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,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;
}
Code to open the 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();
}
Try using StreamedContent file = new DefaultStreamedContent(stream,"application/octet-stream", arquivo.getName());
This is how I do it in my application:
I do not use datatable. I use ui:repeat the iterates through a list of ArquivoAnexo.
<ui:repeat value="#{lista}" var="arquivo" varStatus="status">
<h:commandLink actionListener="#{cadastrarBean.prepararDownloadArquivo(arquivo)}" styleClass="downloadArquivoAnexo">
<p:fileDownload value="#{cadastrarBean.arquivoParaDownload}"/>
</h:commandLink>
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);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With