繁体   English   中英

Primefaces p:带数据表的fileDownload

[英]Primefaces p:fileDownload with datatable

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

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

任何人?

(如果我取消注释注释部分,filedownload不起作用,如果我保留它,我不能通过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,这个问题得到了解决

这是我的代码修复:

用于填充数据表的代码:

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

尝试使用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);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM