我有一个返回文件夹所有文件的数据表,可以使用primefaces p:filedownload资源下载该文件。
它工作正常,但是当加载代码时我无法修改文件,因为FileInputStream是oppened。 如果我在数据表加载期间关闭FileInputStream,则p:filedownload不起作用
任何?
(如果我取消注释评论部分,文件下载不起作用,如果我保留它,我不能通过窗口编辑文件)
爪哇:
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>
谢谢!
答案 0 :(得分:4)
这个问题已经解决了,感谢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>
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();
}
答案 1 :(得分:3)
尝试使用 StreamedContent file = new DefaultStreamedContent(stream,“application / octet-stream”,arquivo.getName());
这就是我在申请中的表现:
我不使用数据表。我使用ui:通过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);
}