Primefaces p:带数据表的fileDownload [英] Primefaces p:fileDownload with datatable
问题描述
我有一个返回文件夹所有文件的数据表,可以使用primefaces p:filedownload资源下载该文件。
I got a datatable that return all files of a folder, and that file can be downloaded using the primefaces p:filedownload resource.
它工作正常,但是当代码加载时,我无法修改文件,因为FileInputStream是oppened。
如果我在数据表加载期间关闭FileInputStream,p:filedownload不起作用
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
任何人?
(如果我取消注释注释部分,filedownload不起作用,如果我保留它,我不能通过windows编辑文件)
(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>
谢谢!!
推荐答案
这个问题已经解决了,感谢sabrina.bettini
The question was solved, thanks to sabrina.bettini
这里我的代码已修好:
填写数据表的代码:
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;
}
使用actionlistener打开文件的代码:
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:
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();
}
这篇关于Primefaces p:带数据表的fileDownload的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!