这是我在论坛上的第一个问题,我希望你能帮助我解决这个问题。 我的escenary:
- 基于我想从浏览器下载的文件
- 我正在使用 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>
我的下载管理器
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 字节的文件,但是当下载完成时,我唯一知道的是文件名和数据类型
最佳答案
看这段代码:
try {
stream.read(bytes);
} catch (IOException ex) {
Logger.getLogger(...)
}
您正在从流中读取 - 返回包含开始数据的字节数组。你为什么要那样做?它将流定位在末尾。
只需删除那个 block ,它可能就没问题了。
关于java - StreamedContent : Download is complete, 但文件为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11747320/