gpt4 book ai didi

java - Primefaces p :fileDownload with datatable

转载 作者:塔克拉玛干 更新时间:2023-11-01 22:29:37 25 4
gpt4 key购买 nike

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

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

有人吗?

(如果我取消评论部分,文件下载不起作用,如果我保留它,我不能通过窗口编辑文件)

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

这是我修复的代码:

填充数据表的代码:

    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:

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>

文件助手:

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();
}

关于java - Primefaces p :fileDownload with datatable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14144306/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com