2012-07-31 104 views
1

这是我在论坛中的第一个问题,希望你能帮助我解决这个问题。 我escenary:StreamedContent:下载完成,但文件为空

  1. 的基础上的文件,我想从一个浏览器下载
  2. 我使用primefaces,FileDownload和StreamedContent

问题

问题是,下载一个文件0Bytes

我view.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> 

我DownloadManger

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个字节长度的文件,但是当下载完成的唯一东西,我有文件名和数据类型

回答

3

看看这段代码:

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

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

只要删除该块,它可能会很好。

+0

谢谢!问题解决了,事实是,我意识到这个代码,因为离开了许多变化。 – 2012-08-01 15:47:24