【问题标题】:unable to download excel file using JSF [duplicate]无法使用 JSF 下载 excel 文件 [重复]
【发布时间】:2017-06-08 00:21:46
【问题描述】:

您好,我一直在尝试编写代码,以便我可以生成一个 excel 表,并在用户单击下载按钮时下载它....我已经成功生成了 excel 表,但我尝试下载相同的表但我没有成功。

我使用的方法是:

public void download() throws IOException {
    File file = new File("D:\\pdf\\carrierReport7.xls");

    FacesContext facesContext = FacesContext.getCurrentInstance();

    HttpServletResponse response = (HttpServletResponse) facesContext.getExternalContext().getResponse();

    response.setHeader("Content-Disposition", "attachment;filename=" + file.getName());
    response.setHeader("Content-Type", "application/vnd.ms-excel");

    OutputStream outputStream = response.getOutputStream();

    FileInputStream fileInputStream = new FileInputStream(file);

    byte[] bytesBuffer = new byte[2048];

    int bytesRead = 0;

    while ((bytesRead = fileInputStream.read(bytesBuffer)) > 0) {
        outputStream.write(bytesBuffer, 0, bytesRead);
    }

    outputStream.flush();

    fileInputStream.close();
    outputStream.close();

    facesContext.responseComplete();
}

jsf 命令:

【问题讨论】:

  • 您是否看到错误/异常?当您尝试此操作时,是否有任何内容实际下载到您的浏览器?
  • 尝试在 stackoverflow 中找到其中一个重复项,并检查其中哪些部分对您有帮助。 Nicolas Smith 是对的……您可以而且应该提供更多信息。
  • 没有错误。它只是处理请求而没有响应。也没有下载。我尝试了stackoverflow中已经给出的不同代码,但我仍然面临同样的问题。我是否需要对 web.xml 或浏览器设置进行任何更改

标签: java jsf


【解决方案1】:

不知道你有没有勾选这个,但是必须在commandButton中声明ajax="false"...

<p:commandButton ajax="false" 
                 ..........
</p:commandButton>

【讨论】:

    【解决方案2】:

    这就是我在 JSF 2 中做类似事情的方式,不确定您使用的是哪个版本。

    public void downloadAttachment(Attachment attachment) throws IOException {
        FacesContext fc = FacesContext.getCurrentInstance();
        ExternalContext ec = fc.getExternalContext();
    
        ec.responseReset(); // 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.setResponseContentType(ec.getMimeType(attachment.getFilename()));        
        ec.setResponseContentLength(attachment.getFilesizeBytes().intValue()); // 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.setResponseHeader("Content-Disposition", "attachment; filename=\"" + attachment.getFilename() + "\""); // 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.
    
        OutputStream output = ec.getResponseOutputStream();
        output.write(attachment.getFileData());
    
        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. }
    
    }
    

    【讨论】:

    • 我试过你提到的代码,但仍然是同样的问题。它没有下载任何东西。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-26
    • 2018-02-03
    • 2012-01-09
    • 2019-01-27
    相关资源
    最近更新 更多