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

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

问题描述

所有人!

我有麻烦了.我试图将excel文件保存在jsf Web应用程序中. 我通过utils生成了文件并尝试获取保存"窗口,但失败了.

I have a trouble. I tried to save excel file in jsf web application. I generated file by my utils and trying to get "save" window, but I failed.

这是我的代码:

  <div>
  <h:commandButton value="Apply" actionListener="#{hornPhonesBean.generateReport}"/>
  </div>

和:

   public void generateReport(ActionEvent event) {
    System.out.println("GENERATE REPORT FROM = " + this.dateFrom + "; TO = " + this.dateTo);

    try {
        XSSFWorkbook workbook = (XSSFWorkbook) HornReportGenerator.getWorkbook(null, null);
        String fileName = "1.xlsx";

        FacesContext fc = FacesContext.getCurrentInstance();
        ExternalContext ec = fc.getExternalContext();

        // Some JSF component library or some Filter might have set some headers in the buffer beforehand. We want to get rid of them, else it may collide.
        ec.responseReset(); 

        // Check http://www.w3schools.com/media/media_mimeref.asp for all types. Use if necessary ExternalContext#getMimeType() for auto-detection based on filename.
        ec.setResponseContentType("application/vnd.ms-excel"); 

        // Set it with the file size. This header is optional. It will work if it's omitted, but the download progress will be unknown.
        //ec.setResponseContentLength(contentLength); 

        // The Save As popup magic is done here. You can give it any file name you want, this only won't work in MSIE, it will use current request URL as file name instead.
        ec.setResponseHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\""); 

        OutputStream output = ec.getResponseOutputStream();
        workbook.write(output); 
        output.flush();
        output.close();

        fc.responseComplete(); // Important! Otherwise JSF will attempt to render the response which obviously will fail since it's already written with a file and closed.
        System.out.println("END");
    } catch (Exception e) {
        e.printStackTrace();
    }
}

我在这里和其他论坛上都读过建议-每个人都说我不应该使用,但是我根本没有使用它.

I read suggestions here and from another forums - everyone says I shouldnt use , but I didn't use it at all.

然后我认为问题可能出在

Then I thought that the problem could be in the

 <ice:form>, 

我存放

 <h:commandButton>, 

然后我更改为

 <h:form>, 

但这没有帮助.

也许是请求中的问题-它具有标头Faces-Request部分/ajax.但是我不确定.

Maybe the problem in the request - it has header Faces-Request partial/ajax. But I am not sure.

请给我一些想法-我已经花了4个小时来解决这个疯狂的jsf下载问题

Please give me some ideas - I already spent 4 hours for this crazy jsf download issue)

推荐答案

可能是请求中的问题-它具有标头Faces-Request部分/ajax.但是我不确定.

这表明该请求是ajax请求.您无法通过ajax下载文件. Ajax请求由JavaScript处理,出于明显的安全原因,它没有以编程方式弹出另存为对话框或访问/操纵客户端磁盘文件系统的功能.

This suggests that the request is an ajax request. You can't download files by ajax. Ajax requests are processed by JavaScript which has for obvious security reasons no facilities to programmatically pop a Save As dialogue nor to access/manipulate client's disk file system.

您的代码段未显示您正在使用ajax.也许您过度简化了它,或者您正在使用ICEfaces,它可以在所有标准JSF命令组件上自动启用ajax.

Your code snippet does however not show that you're using ajax. Perhaps you oversimplified it too much or you're using ICEfaces which silently auto-enables ajax on all standard JSF command components.

无论如何,您都需要确保它发送ajax请求.

In any case, you need to make sure that it's not sending an ajax request.

  • How to provide a file download from a JSF backing bean?
  • ICEfaces libary in classpath prevents Save As dialog from popping up on file download

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

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