我有两个输出流(由jasper报告填充数据以生成一个excel和一个csv)。 输出流的填充工作正常,但现在我必须将“流/文件”打包到要下载的zip中。 我怎么能这样做?
只下载一个文件的代码如下:
public void exportInternalEvaluation() { exportStats(ExportingValues.STATISTICS_INTERNAL_EVALUTATION, EXCEL_NAME, solvedDossiers, "application/vnd.ms-excel"); } private void exportStats(ExportingValues exportingValues, String fileName, Collection < ? > collection, String contentType) { OutputStream os = null; try { FacesContext fc = FacesContext.getCurrentInstance(); ExternalContext ec = fc.getExternalContext(); ec.responseReset(); ec.setResponseContentType(contentType); ec.setResponseHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\""); os = ec.getResponseOutputStream(); statsLocal.generateStatStream(collection, os, exportingValues.getJasperFileName(null), exportingValues.getReportingType()); fc.responseComplete(); } }此代码将内容类型设置为excel并允许我下载该文件。 我已经找到了以下内容:
ZipOutputStream zos = new ZipOutputStream(os); //create a zipOutputStream with my responseOutputStream但后来我一直坚持创建ZipEntries 。 如何从2个不同的流创建2个条目(1个excel和1个csv)?
编辑:我真的不想创建2个临时文件并将它们添加到zip中。 我想找到一种方法将2个输出流“添加”到一个zip文件中,创建2个不同的文件,而不必先创建它们(甚至是tempFiles ..)。 如果可能的话......
I've got two outputstreams (filled with data by jasper reports to generate one excel and one csv). The filling of the outputstreams works just fine, but now I have to pack both "streams / files" in to a zip to be downloaded. How could I do this?
The code to download just one file is the following:
public void exportInternalEvaluation() { exportStats(ExportingValues.STATISTICS_INTERNAL_EVALUTATION, EXCEL_NAME, solvedDossiers, "application/vnd.ms-excel"); } private void exportStats(ExportingValues exportingValues, String fileName, Collection < ? > collection, String contentType) { OutputStream os = null; try { FacesContext fc = FacesContext.getCurrentInstance(); ExternalContext ec = fc.getExternalContext(); ec.responseReset(); ec.setResponseContentType(contentType); ec.setResponseHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\""); os = ec.getResponseOutputStream(); statsLocal.generateStatStream(collection, os, exportingValues.getJasperFileName(null), exportingValues.getReportingType()); fc.responseComplete(); } }This code is setting the content type to excel and lets me download the file. I've already found the following:
ZipOutputStream zos = new ZipOutputStream(os); //create a zipOutputStream with my responseOutputStreambut then I'm stuck on creating the ZipEntries. How can I create 2 entries (1 excel and 1 csv) from 2 different streams?
EDIT: And I don't really want to create 2 tempfiles and add them to the zip. I'd like to find a way to "add" the 2 outputstreams to a zip file creating 2 different files without having to create them first (even tempFiles..). If possible though...
最满意答案
您可以使用ZipOutputStream修饰响应流,并将生成器代码写入,以确保在每次调用之间创建新条目:
try (OutputStream os = ec.getResponseOutputStream(); ZipOutputStream zout = new ZipOutputStream(os)) { zout.putNextEntry(new ZipEntry("foo.xls")); generate(zout, "Excell gen call args"); zout.closeEntry(); zout.putNextEntry(new ZipEntry("foo.csv")); generate(zout, "CSV gen call args"); zout.closeEntry(); }You can decorate the response stream with a ZipOutputStream and have your generator code write to that ensuring that you create new entries between each call:
try (OutputStream os = ec.getResponseOutputStream(); ZipOutputStream zout = new ZipOutputStream(os)) { zout.putNextEntry(new ZipEntry("foo.xls")); generate(zout, "Excell gen call args"); zout.closeEntry(); zout.putNextEntry(new ZipEntry("foo.csv")); generate(zout, "CSV gen call args"); zout.closeEntry(); }更多推荐
发布评论