【问题标题】:Error trying to download content with PrimeFaces <p:fileDownload> java.io.IOException: Error in streaming dynamic resource Exception尝试使用 PrimeFaces <p:fileDownload> java.io.IOException 下载内容时出错:流式动态资源异常中的错误
【发布时间】:2022-01-14 17:47:03
【问题描述】:

各位程序员大家好。我正在尝试在带有 PrimeFaces 10 的 JSF 应用程序中使用 &lt;p:fileDownload&gt;。我想下载一个纯数据文件(不是图形或图像),它作为字节数组从数据库中获取。

在视图中我有下载按钮:

<h:form enctype="multipart/form-data">
...
    <p:commandButton value="Descargar..." 
                     rendered="#listaMaterialesBacking.listaSeleccionada.nombreArchivoDiseno != null}">
        <p:fileDownload value="#{listaMaterialesBacking.descargaDiseno}"/>
    </p:commandButton>
</h:form>

在(视图范围的)托管 bean 中我有:

@Named(value = "listaMaterialesBacking")
@ViewScoped
public class ListaMaterialesBacking implements Serializable {
...
    public StreamedContent getDescargaDiseno()
    { 
        //we fetch the byte array from the db
        byte[] archivoD = getListaMaterialesFacade().recuperarArchivoDiseno(listaSeleccionada.getId()); 
        
        StreamedContent file = DefaultStreamedContent.builder()
                                .name(listaSeleccionada.getNombreArchivoDiseno())
                                .contentType("application/octet-stream")
                                .stream(() -> new ByteArrayInputStream(archivoD))
                                .build();
    
        return file;
    }
}

然后,当我单击下载按钮时,浏览器会回复一个 15KB 的文件下载,其中包含的 HTML 代码比包含的内容:

错误处理请求上下文路径:/obrasttt-1.0-SNAPSHOT

Servlet 路径:/faces

路径信息:/javax.faces.resource/dynamiccontent.properties

查询字符串: ln=primefaces&v=10.0.0-RC1&pfdrid=471e3b498e52f03eee44242c8050ee23&pfdrt=sc&pfdrid_c=false&uid=62d196e9-84c2-455e-81b7-c089f53b80f6

堆栈跟踪:

java.io.IOException:流式传输动态资源时出错 org.primefaces.application.resource.StreamedContentHandler.handle(StreamedContentHandler.java:120) ...

存储在数据库中的文件不是很大,大约有 500kB 大小。

我正在使用:

Primefaces 10.0.0-RC1.jar

JDK 1.8

JSF 2.3 (mojarra)

野蝇 25

MySQL 5.7

Apache NetBeans 12.4

我该怎么办?提前致谢

【问题讨论】:

    标签: jsf primefaces


    【解决方案1】:

    你正在使用Ajax downloading,它使用dynamic content streaming,它使用does not support @ViewScoped

    有关工作示例,请参阅showcase

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-15
      • 1970-01-01
      • 2016-06-22
      • 2019-07-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多