java - StreamedContent : Download is complete, 但文件为空

标签 java postgresql primefaces

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

  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>

我的下载管理器

    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/

相关文章:

java - 为抽象 java 类制作 JUnit 测试用例?

java - Struts Action 代理 : how to set a method?

PHP base64_encode PDF 文件损坏

sql - 在 SQL 语句轨中组合 Ruby 变量来查找两个日期之间的天数差异

jsf-2 - 如何调整jsf2 primefaces中选项卡的宽度和高度?

maven - java.lang.ClassCastException : Cannot cast com. sun.faces.config.FacesInitializer 到 javax.servlet.ServletContainerInitializer

java - 使用java找到两个最小的数字?

Postgres 的 Django 数据库模型创建和索引

java - 如何在 PrimeFaces 中使用未缩小版本的 jQuery?

java - 配合 libGDX 和 Jetbrains MPS