3

フォルダのすべてのファイルを返すデータテーブルを取得しました。そのファイルは、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>

ありがとうございました !!

4

2 に答える 2

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();
}
于 2013-01-04T17:21:30.123 に答える
3

StreamedContent file = new DefaultStreamedContent(stream、 "application / octet-stream"、arquivo.getName());を使用してみてください。


これは私のアプリケーションでそれを行う方法です:

私はdatatableを使用していません。私は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);
}
于 2013-01-03T18:07:47.860 に答える