Java 使用 JSF 下载文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3428039/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Download a file with JSF?
提问by ErVeY
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??
使用 JSF 下载文件的正确方法是什么?,只需放一个指向文件的链接??在这种情况下,我如何获取文件 URL?
i have seen one example using BufferedInputStream:
我见过一个使用 BufferedInputStream 的例子:
http://www.winstonprakash.com/articles/jsf/file_download_link.htm
http://www.winstonprakash.com/articles/jsf/file_download_link.htm
What are the differences?
有什么区别?
Thanks
谢谢
采纳答案by BalusC
If it's a simple file, just place in public webcontent (there where you put your static and JSF files) and create a link.
如果它是一个简单的文件,只需将其放置在公共网络内容中(放置静态文件和 JSF 文件的位置)并创建一个链接。
<h:outputLink value="/files/file.ext">link</h:outputLink>
The servletcontainer will worry about applying the correct headers.
servletcontainer 会担心应用正确的标头。
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 herea simple kickoff example. Also that can simply be linked on the servlet's url-pattern
.
如果由于某些特定原因(例如,在服务器机器上的固定路径中,或在数据库中),它位于公共 web 内容之外,则创建一个 servlet,该 servlet 获取InputStream
它并将其写入OutputStream
响应的 至少沿Content-Type
,Content-Disposition
和Content-Length
标题。您可以在此处找到一个简单的启动示例。也可以简单地链接到 servlet 的url-pattern
.
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 特定的请求参数,那么您也可以在受h:commandLink
或绑定的托管 bean 操作中执行此操作h:commandButton
,但您只需要确保FacesContext#responseComplete()
在 bean 的操作方法的末尾调用以防止 JSF从手中掌握导航。可以重用相同类型的 servlet 代码来流式传输文件。您可以在此答案中找到启动示例。
回答by Marco Paulo Ollivier
I needed to make a similar code to download a file via JSF
我需要制作一个类似的代码来通过 JSF 下载文件
That's my download button in my JSF page
这是我的 JSF 页面中的下载按钮
<h:commandButton value="Download" action="#{helloBean.downloadFile}" />
And it's my Java Code
这是我的 Java 代码
public void downloadFile() {
File file = new File("/home/marco/file.txt");
HttpServletResponse response = (HttpServletResponse) FacesContext.getCurrentInstance().getExternalContext().getResponse();
response.setHeader("Content-Disposition", "attachment;filename=file.txt");
response.setContentLength((int) file.length());
ServletOutputStream out = null;
try {
FileInputStream input = new FileInputStream(file);
byte[] buffer = new byte[1024];
out = response.getOutputStream();
int i = 0;
while ((i = input.read(buffer)) != -1) {
out.write(buffer);
out.flush();
}
FacesContext.getCurrentInstance().getResponseComplete();
} catch (IOException err) {
err.printStackTrace();
} finally {
try {
if (out != null) {
out.close();
}
} catch (IOException err) {
err.printStackTrace();
}
}
}
回答by Manuel Outeiri?o
I have had an error on
我有一个错误
FacesContext.getCurrentInstance().getResponseComplete();
from the type
从类型
java.lang.IllegalStateException: getOutputStream() has already been called for this response
and i solved it:
我解决了它:
JSF page:
JSF 页面:
<h:commandButton action="#{bean.downloadFile}" id="downloadBtn" value="Download"/>
Bean method:
豆法:
public void downloadFile(File file) {
FacesContext facesContext = FacesContext.getCurrentInstance();
HttpServletResponse response = (HttpServletResponse) facesContext.getExternalContext().getResponse();
response.setHeader("Content-Disposition", "attachment;filename=file.txt");
response.setContentLength((int) file.length());
FileInputStream input= null;
try {
int i= 0;
input = new FileInputStream(file);
byte[] buffer = new byte[1024];
while ((i = input.read(buffer)) != -1) {
response.getOutputStream().write(buffer);
response.getOutputStream().flush();
}
facesContext.responseComplete();
facesContext.renderResponse();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if(input != null) {
input.close();
}
} catch(IOException e) {
e.printStackTrace();
}
}
}