我正在尝试使用richfaces下载压缩为存档的XML文件。我已经完成了下载文件的常用方法。我需要使用.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文件zip: -
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
XLSX文件zip: -
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