2

当相应文件丢失时,处理点击文件下载链接的最佳做法是什么?

具体情况是DB中存在附件实体,只指向文件名,文件存储路径可以单独/单独配置。这适用于旧版应用程序,必须得到支持。

这可能吗?这样的代码看起来如何?我试过

    if ( file.canRead() )
    {
        byte[] data = FileUtils.readFileToByteArray( file );

        // goes to code like here: http://balusc.blogspot.de/2006/05/pdf-handling.html
        downloadFile( attachment.getFileName(), data );
    }
    else
    {
        this.log.errorv( "Attachment {0} not found in configured storage path {1}", file, this.storagePath );

        FacesContext facesContext = FacesContext.getCurrentInstance();

        facesContext.addMessage( null,
                                 new FacesMessage( FacesMessage.SEVERITY_ERROR, "Failed.",
                                                   "Downlosding file " + file + " failed! File doesn't exist in the configured storage path " + this.storagePath ) );

        // ???
        facesContext.responseComplete();
    }

但这会导致

XML-Verarbeitungsfehler: Kein Element gefunden
Adresse: https://localhost:8181/cmc-compliance/view/prototype/collisionManager.xhtml
Zeile Nr. 1, Spalte 1:

(天哪<rant>,我讨厌……呃不喜欢int18ned错误消息……有人应该摆脱认为这是个好主意的人…… )</rant>

好的,上面的意思类似于“XML 处理错误:未找到元素 + 第 1 行,第 1 列”

我的代码显然不是正确的方法......

我在数据表中使用 JSF 代码:

<h:commandLink action="#{attachmentManager.downloadAttachment(att)}">
    <h:graphicImage library="images/icons" name="page_white.png" />
    <h:outputText value="#{att.fileName}" />
</h:commandLink>

理想情况下,我想要的是显示一条 JSF 消息(或 PrimeFaces 咆哮),然后将页面保持原样,即无需再次在同一页面上发出完整的请求。

你怎么做到这一点?

4

1 回答 1

2

facesContext.responseComplete();你基本上是在阻止 JSF 呈现响应。因此,客户端检索到一个完全空的响应。网络浏览器正试图充分利用它。网络浏览器只知道请求的资源具有.xhtml文件扩展名。所以网络浏览器假定它是一些 XML 内容。然后网络浏览器尝试将空响应解析为 XML。但由于根本没有 XML 元素,它因给定错误而惨遭失败。即要求 XML 格式良好的文档具有至少一个根 XML 元素。

删除该facesContext.responseComplete();行并以通常的方式从该方法返回。您应该facesContext.responseComplete();在您自己已经写入响应时使用,例如在这种特殊情况下提供文件下载。


OMG,我讨厌 int18ned 错误消息...有人应该摆脱认为这是个好主意的人...

只需在平台特定设置中相应地更改操作系统平台默认区域设置。对于 Windows,可以在此处找到带有屏幕截图的相关答案:Getting OS language in java。如果程序本身(例如 Firefox/Chrome)也有一些与语言环境相关的设置,您可能还想在程序本身中更改它。

于 2012-10-31T16:15:12.480 回答