FileDownload 解析为 null

FileDownload resolves into null

我有一个无法正常下载的文件。我按照 this 线程中提供的说明进行操作,但下载的文件只是一个名称为 null 的文件,但大小与我最初想要下载的文件大小相同。我使用的命令是

<p:commandLink ajax="false">
                    Download
                    <p:fileDownload value="#{dtEditView.file}"/>
       </p:commandLink>

我的 bean 包含以下内容

public StreamedContent getFile() throws IOException {
    path = "C:\test.jpeg";
    contentType = FacesContext.getCurrentInstance().getExternalContext().getMimeType(path);
    return new DefaultStreamedContent(new FileInputStream(path), contentType);
}

我做错了什么,我错过了什么?我正在使用 Primefaces 5.2。

编辑:我将文件重命名为它应该的文件扩展名,它似乎是我想要的。剩下的问题还在吗?

  1. 为什么改名为null?
  2. 如何自动设置正确的文件扩展名?

只需将要用于下载的文件名添加到 DefaultStreamedContent 的构造函数中即可:

return new DefaultStreamedContent(new FileInputStream(path), contentType, "filename.jpeg");

或与资源保持相同的名称:

return new DefaultStreamedContent(new FileInputStream(path), contentType, path.getFileName().toString());

与展示柜中的豆子进行比较:

http://www.primefaces.org/showcase/ui/file/download.xhtml