用JSF下载文件? [英] Download a file with 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
有什么区别?
谢谢
推荐答案
如果它是一个简单的文件,只是放在公共web内容(在那里你放置你的静态和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.
如果由于某些特定原因(例如在服务器机器或数据库中的固定路径),它位于公共Web内容之外,则创建一个获取 InputStream
,并将其写入 OutputStream $ c至少$ code> Content-Type
, Content-Disposition
和 Content-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具体的请求参数,那么你也可以在受到管理的bean操作中执行,该操作由 h:commandLink
或 h:commandButton
,但您只需要确保您调用 FacesContext#responseComplete()
在bean的动作方法的结尾,以防止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屋!