加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java – Primefaces p:带数据表的fileDownload

发布时间:2020-12-15 08:49:20 所属栏目:Java 来源:网络整理
导读:我有一个返回文件夹的所有文件的数据表,并且可以使用primefaces p:filedownload资源下载该文件. 它工作正常,但是当加载代码时我无法修改文件,因为FileInputStream是oppened. 如果我在数据表加载期间关闭FileInputStream,则p:filedownload不起作用 任何人?
我有一个返回文件夹的所有文件的数据表,并且可以使用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,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();
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读