使用 JSF 下载文件? [英] Download a file with JSF?

查看:29
本文介绍了使用 JSF 下载文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 JSF 下载文件的正确方法是什么?,只需放一个指向文件的链接??在这种情况下,我如何获取文件 URL?

which is the right way to download a file using JSF?, just putting a link to the file ?? in that case how do i get the file URL??

我见过一个使用 BufferedInputStream 的例子:

i have seen one example using BufferedInputStream:

http://www.winstonprakash.com/articles/jsf/file_download_link.htm

有什么区别?

谢谢

推荐答案

如果它是一个简单的文件,只需放置在公共网页内容中(在那里放置静态文件和 JSF 文件)并创建一个链接.

If it's a simple file, just place in public webcontent (there where you put your static and JSF files) and create a link.

<h:outputLink value="/files/file.ext">link</h:outputLink>

servletcontainer 会担心应用正确的标头.

The servletcontainer will worry about applying the correct headers.

如果由于某些特定原因(例如在服务器机器上的固定路径中,或在数据库中),它位于公共网络内容之外,则创建一个获得 InputStream 并将其写入 OutputStream 至少沿着响应Content-TypeContent-DispositionContent-Length 标头.您可以在此处找到一个简单的启动示例.也可以简单地链接到 servlet 的 url-pattern.

If it's located outside the public webcontent for some specific reasons (e.g. in a fixed path at server machine, or in a database), then create a servlet which gets an InputStream of it and writes it to the OutputStream of the response along at least the Content-Type, Content-Disposition and Content-Length headers. You can find here a simple kickoff example. Also that can simply be linked on the servlet's url-pattern.

如果它是动态生成的并且取决于 JSF 特定的请求参数,那么您也可以在由 h:commandLinkh:commandButton 绑定的托管 bean 操作中执行此操作,但你只需要确保你调用 FacesContext#responseComplete() 在 bean 的 action 方法的末尾,以防止 JSF 控制导航.可以重用相同类型的 servlet 代码来流式传输文件.您可以在 这个答案.

If it's to be dynamically generated and depending on the JSF specific request parameters, then you can also do so in a managed bean action which is bound by h:commandLink or h:commandButton, but you only need to ensure that you call FacesContext#responseComplete() at end of bean's action method to prevent JSF from taking the navigation in hands. The same kind of servlet code can be reused to stream the file. You can find a kickoff example in this answer.

这篇关于使用 JSF 下载文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆