2012-07-31 105 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