【问题标题】:Primefaces p:fileDownload with datatablePrimefaces p:fileDownload with datatable
【发布时间】:2013-01-03 17:47:09
【问题描述】:

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

它工作正常,但是当加载代码时我无法修改文件,因为 FileInputStream 已打开。 如果我在数据表加载期间关闭 FileInputStream,p: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>

谢谢!!

【问题讨论】:

    标签: java primefaces


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

      尝试使用 StreamedContent 文件 = new DefaultStreamedContent(stream,"application/octet-stream", arquivo.getName());


      这就是我在我的应用程序中的做法:

      我不使用数据表。我使用 ui:repeat 遍历 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);
      }
      

      【讨论】:

      • 你好,萨布丽娜!我得到了同样的错误:“读取错误”。问题就在“new FileInputStream”中...如果我关闭它,我无法打开文件,但如果我保留它,我无法修改文件
      • 我在我的应用程序中做同样的事情,但我从未尝试编辑下载的文件。是否要修改服务器中的文件?或者你只是想下载,然后在本地修改?
      • 我想更改服务器中的文件,因此用户将始终拥有最新的文件
      • 我认为您必须下载文件,然后将其上传回服务器。
      • 你好,Sabrina,使用你的例子,我刚刚发现了如何让它工作! StreamedContent 似乎没有按应有的方式工作,但现在可以了!非常感谢!
      猜你喜欢
      • 2011-11-05
      • 1970-01-01
      • 1970-01-01
      • 2016-04-24
      • 2011-05-02
      • 1970-01-01
      • 2013-10-21
      • 2014-03-19
      • 2015-01-27
      相关资源
      最近更新 更多