StreamedContent:下载完成,但文件为空

时间:2012-07-31 19:13:02

标签: java postgresql primefaces

这是我在论坛上的第一个问题,希望你能帮助我。 我的escenary:

  1. 我想从浏览器下载的文件
  2. 我正在使用primefaces,FileDownload和StreamedContent
  3. 问题

    问题是下载文件0Bytes

    我的观点.xhtml:

    <p:commandButton id="downloadLink" value="Descargar" ajax="false" onclick="PrimeFaces.monitorDownload(start, stop)"   
                                             rendered="#{not anexoController.ingresaDatos}" icon="ui-icon-arrowthichk-s">  
                                <p:fileDownload value="#{anexoController.file}" />  
                            </p:commandButton>
    

    我的下载管理器

        public StreamedContent getFile() {
        file = null;
        byte[] bytes = anexoActual.getArchivo(); //anexoActual.getArchivo.length = 53508
        String nombre = anexoActual.getNombre(); 
        String formato = anexoActual.getFormato();
        InputStream stream = new ByteArrayInputStream(bytes);
        try {
            stream.read(bytes);
        } catch (IOException ex) {
            Logger.getLogger(AnexoController.class.getName()).log(Level.SEVERE, null, ex);
        }
        file = new DefaultStreamedContent(stream, formato, nombre);
        return file; //file.steam.buf.length = 53508
    }
    

    正如您所看到的,文件到达文件的长度为53508字节,但是下载完成时我唯一拥有的文件名和数据类型

1 个答案:

答案 0 :(得分:4)

看看这段代码:

try {
    stream.read(bytes);
} catch (IOException ex) {
    Logger.getLogger(...)
}

您从流中读取 - 返回包含要开始的数据的字节数组。你为什么这样做?它将流定位在最后。

只需移除该块即可,它可能没问题。