通过jsf下载XML文件

通过jsf下载XML文件,xml,jsf,download,Xml,Jsf,Download,我正在尝试使用richfaces下载压缩为归档文件的XMLs文件。我已经用通常的方式下载了文件。我需要下载带有.xlsx文件的zip文件。这段代码同样适用于xlsx文件的压缩归档 我有一个throwDownload(),如下所示 public void throwDownload(ByteArrayOutputStream baos, String fileName) throws IOException { FacesContext facesContext = FacesCon

我正在尝试使用richfaces下载压缩为归档文件的XMLs文件。我已经用通常的方式下载了文件。我需要下载带有.xlsx文件的zip文件。这段代码同样适用于xlsx文件的压缩归档

我有一个throwDownload(),如下所示

  public void throwDownload(ByteArrayOutputStream baos, String fileName) throws IOException {

    FacesContext facesContext = FacesContext.getCurrentInstance();
    ExternalContext externalContext = facesContext.getExternalContext();
    HttpServletResponse response = (HttpServletResponse) externalContext
            .getResponse();


    response.reset();
    response.setContentType("application/zip");
    response.setContentLength( baos.size() );
    response.setHeader("Content-disposition", "attachment; filename=\""
            + fileName + "\"");

    BufferedOutputStream output = null;
    final int buffersize = 20480;

    output = new BufferedOutputStream(response.getOutputStream(),
            buffersize);
    output.write(baos.toByteArray());

    facesContext.responseComplete();

}
同样的代码适用于包含.xlsx文件的zip文件。对于包含.xml文件的zip,浏览器会显示“另存为”对话框,但下载的文件没有字节。fiddler中两个请求的响应头如下所示:

XML压缩文件:-

HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Content-disposition: attachment; filename="CRS_Files.zip"
Content-Type: application/zip
Content-Length: 1426
Date: Sun, 06 Apr 2014 14:42:50 GMT
HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Content-disposition: attachment; filename="DataCheckReports.zip"
Content-Type: application/zip
Transfer-Encoding: chunked
Date: Sun, 06 Apr 2014 15:00:37 GMT
XLSX文件压缩包:-

HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Content-disposition: attachment; filename="CRS_Files.zip"
Content-Type: application/zip
Content-Length: 1426
Date: Sun, 06 Apr 2014 14:42:50 GMT
HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Content-disposition: attachment; filename="DataCheckReports.zip"
Content-Type: application/zip
Transfer-Encoding: chunked
Date: Sun, 06 Apr 2014 15:00:37 GMT

您应该
flush()。在
output.write()
之后添加
output.flush()
。稍后我会发布一个答案