【问题标题】:Download a file with JSF? [duplicate]用 JSF 下载文件? [复制]
【发布时间】:2011-03-26 13:50:07
【问题描述】:

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

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

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

有什么区别?

谢谢

【问题讨论】:

    标签: java jsf download


    【解决方案1】:

    我遇到了一个错误

    FacesContext.getCurrentInstance().getResponseComplete(); 
    

    从类型

    java.lang.IllegalStateException: getOutputStream() has already been called for this response 
    

    我解决了:

    JSF 页面:

    <h:commandButton action="#{bean.downloadFile}" id="downloadBtn" value="Download"/>
    

    豆方法:

    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();
            }
        }
    }
    

    【讨论】:

    • 我说试试看,因为我没有时间解释清楚,帖子的标题是[用 JSF 下载文件?]。我已经对其进行了测试并且它有效,所以如果你没有测试,请不要告诉我认为......
    【解决方案2】:

    我需要编写类似的代码来通过 JSF 下载文件

    这是我 JSF 页面中的下载按钮

    <h:commandButton value="Download" action="#{helloBean.downloadFile}" />
    

    这是我的 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();  
            }  
        }  
    
    }
    

    【讨论】:

    • 命令按钮的action方法不应该返回String吗?
    • 我认为这个答案有错误。最后你必须打电话给FacesContext.getCurrentInstance().responseComplete()(而不是getResponseComplete
    【解决方案3】:

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

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

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

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

    如果它是动态生成的并且取决于JSF特定的请求参数,那么你也可以在一个受h:commandLinkh:commandButton绑定的托管bean动作中这样做,但你只需要确保你调用FacesContext#responseComplete() 在 bean 的操作方法末尾,以防止 JSF 将导航拿在手中。可以重复使用相同类型的 servlet 代码来流式传输文件。您可以在 this answer 中找到启动示例。

    【讨论】:

    • O.o Thx BalusC 我整个周末都无法连接,但我要感谢你的帮助 =D 我真的很感激
    猜你喜欢
    • 2015-10-31
    • 2018-02-03
    • 2017-06-08
    • 2012-08-23
    • 1970-01-01
    • 1970-01-01
    • 2017-12-18
    相关资源
    最近更新 更多