Zip Outputstream已关闭。java.io.IOException:流已关闭

Zip Outputstream已关闭。java.io.IOException:流已关闭,java,Java,如何修复此异常?我需要将多个xlsx添加到zip。第一个xlsx被添加到列表中,但第二个xlsx抛出此错误: public void downloadData() throws Exception { FacesContext fc = FacesContext.getCurrentInstance(); setSourceList(new ArrayList<String>()); setTargetList(new ArrayList<String

如何修复此异常?我需要将多个xlsx添加到zip。第一个xlsx被添加到列表中,但第二个xlsx抛出此错误:

public void downloadData() throws Exception {

    FacesContext fc = FacesContext.getCurrentInstance();
    setSourceList(new ArrayList<String>());
    setTargetList(new ArrayList<String>());

    List<String> tempList = dualList.getTarget();
    System.out.println(tempList.size());

    if (tempList != null && tempList.size() > 0) {

        ExternalContext ec = fc.getExternalContext();
        ec.responseReset();

        ec.setResponseContentType("application/zip");
        ec.setResponseHeader("Content-Disposition", "attachment; filename=\"Export.zip\"");
        ByteArrayOutputStream outByteStream = new ByteArrayOutputStream();
        ZipOutputStream zip = new ZipOutputStream(outByteStream);
        OutputStream outStream = ec.getResponseOutputStream();

        for (int i = 0; i < tempList.size(); i++) {

            outByteStream = new ByteArrayOutputStream();
            //zip = new ZipOutputStream(outByteStream);
            // outStream = ec.getResponseOutputStream();

            int id = ownerNameIdMap.get(tempList.get(i));
            String oName = tempList.get(i);
            String fileName = oName + ".xlsx";

            if (petList != null && petList.size() > 0) {
                petList.clear();
            }

            workBook = new XSSFWorkbook();

            OwnerModel ownerModel = ownerMap.get(id);
            ownerFirstName = ownerModel.getFirstName();
            ownerLastName = ownerModel.getLastName();
            workSheet = workBook.createSheet(ownerLastName + ", " + ownerFirstName);
            petList = iOwnerRepository.getActivePetsOfOwner(ownerModel.getId());
            petCount = petList.size();
            createMasterSheet();
            renderSheet(ownerModel);

            workBook.write(outByteStream);
            //addEntry(zip, fileName, outByteStream);
            ZipEntry entry = new ZipEntry(fileName);
            zip.putNextEntry(entry);
            System.out.println(fileName);
            zip.write(outByteStream.toByteArray());
            zip.closeEntry();
            workBook.write(zip);
            outStream.write(outByteStream.toByteArray());


        }

        outStream.flush();
        outStream.close();
        zip.close();
        fc.responseComplete();

    }

}

尝试合并使用
outByteStream
。在
for
循环之前打开它,并用它初始化
zip
,但在
for
循环中,您将另一个
ByteArrayOutputStream
实例分配给
OutByTestStream
并随后使用此实例。我不确定这是否会导致您的异常,但可能值得尝试…

您好,我有这个问题,只将第一个文件添加到zip流并抛出stream close异常,因此我首先将文件转换为字节数组,而不是在zip中使用stream。write()方法,更改代码如下

private byte[] _toByteArray(InputStream in) throws IOException {
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        byte[] buffer = new byte[1024];
        int len;

        while ((len = in.read(buffer)) != -1) {
            os.write(buffer, 0, len);
        }

        return os.toByteArray();
    }

public byte[] getFileAsZip(
        long id) {

        List<DLFileEntry> dlFileEntries =
            getDLFileEntries(
                id);

        ByteArrayOutputStream baos = new ByteArrayOutputStream();

        ZipOutputStream zout = new ZipOutputStream(baos);

        try {
            for (DLFileEntry dl : dlFileEntries) {
                byte[] dlBytes = _toByteArray(dl.getContentStream());//output of getContenStream is InputSream type

                ZipEntry zip = new ZipEntry(dl.getFileName());

                zout.putNextEntry(zip);

                zout.write(dlBytes);
                zout.closeEntry();
            }
        }
        catch (Exception e) {
            System.out.println(e.getMessage());
        }

        try {
            zout.flush();
            zout.close();
        }
        catch (IOException e) {
            e.printStackTrace();
        }

        return baos.toByteArray();
    }

我希望这个答案有帮助

代码片段中的哪一行代表第218行?zip.putNextEntry(条目);这line@Thomas你有办法吗?不确定。找到了一些值得研究的东西。。。请看我的答案。除此之外,不,对不起…谢谢。我会尝试一下,但没有成功。显示无法打开存档文件。
private byte[] _toByteArray(InputStream in) throws IOException {
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        byte[] buffer = new byte[1024];
        int len;

        while ((len = in.read(buffer)) != -1) {
            os.write(buffer, 0, len);
        }

        return os.toByteArray();
    }

public byte[] getFileAsZip(
        long id) {

        List<DLFileEntry> dlFileEntries =
            getDLFileEntries(
                id);

        ByteArrayOutputStream baos = new ByteArrayOutputStream();

        ZipOutputStream zout = new ZipOutputStream(baos);

        try {
            for (DLFileEntry dl : dlFileEntries) {
                byte[] dlBytes = _toByteArray(dl.getContentStream());//output of getContenStream is InputSream type

                ZipEntry zip = new ZipEntry(dl.getFileName());

                zout.putNextEntry(zip);

                zout.write(dlBytes);
                zout.closeEntry();
            }
        }
        catch (Exception e) {
            System.out.println(e.getMessage());
        }

        try {
            zout.flush();
            zout.close();
        }
        catch (IOException e) {
            e.printStackTrace();
        }

        return baos.toByteArray();
    }

byte[] zipFile =
            getFileAsZip(id);

        String fileName = "test";

        String dispositionDesc = String.format(
            "attachment;filename=\"%s\"", fileName);

        Response.ResponseBuilder responseBuilder = Response.ok(zipFile);

        responseBuilder.header("Content-Disposition", dispositionDesc);
        responseBuilder.header("Content-Type", "application/zip");