this is my first question in the forum, I hope you can help me with this.
My escenary:
- A file on the basis that I want to download from a browser
- I’m using primefaces, FileDownload and StreamedContent
The problem
The problem is that downloading a file 0Bytes
My 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>
My 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
}
As you can see the file reaches the file with a length of 53508 bytes, but when the download is complete the only thing that I have the file name and data type
Look at this code:
You’re reading from the stream – back into the byte array which contains the data to start with. Why are you doing that? It’s positioning the stream at the end.
Just remove that block and it may well be fine.