如何使用两个outputStream创建zip文件(使用JSF)(How to create zip file with two outputStreams (working with JSF))

编程入门 行业动态 更新时间:2024-10-26 17:29:16
如何使用两个outputStream创建zip文件(使用JSF)(How to create zip file with two outputStreams (working with JSF))

我有两个输出流(由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 responseOutputStream

but 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(); }

更多推荐

本文发布于:2023-08-05 15:46:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1434099.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何使用   两个   文件   outputStream   outputStreams

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!