java.io.IOException: 流关闭 ZipInputStream

java.io.IOException: Stream closed ZipInputStream

下面是代码片段。

    FileInputStream fin = new FileInputStream(zipFile);
    ZipInputStream zin = new ZipInputStream(fin);
    ZipEntry entry = null;
    String routerListUCM = "";

    try {
        entries:

        while ((entry = zin.getNextEntry()) != null) {
            if (entry.getName().startsWith("routes")) {

                BufferedReader in = new BufferedReader(new InputStreamReader(zin, "UTF-8"));

                if (true) {
                    //parse the xml of the route...

                    DOMParser dp = new DOMParser();

                    dp.parse(in);
                    Element e = (Element) dp.getDocument().getFirstChild();

                    String transferid = e.getElementsByTagName("transferId").item(0).getTextContent();
                    System.out.println("transferId=" + transferid);
                    int fileid = Integer.parseInt(transferid.split("-")[1]);
                    System.out.println("fileid=" + transferid);
                    String userList = e.getElementsByTagName("userList").item(0).getTextContent();
                    System.out.println("userList=" + userList);
                    String routeList = e.getElementsByTagName("routeList").item(0).getTextContent();
                    System.out.println("routeList=" + routeList);
                    routerListUCM = routeList;

                    if (routeList.toLowerCase().indexOf(myname().toLowerCase()) == -1) {
                        //my server is not in the current route...
                        //so skip this route table.
                        continue entries;

                    }
                }
            }
        }
    } catch (Exception e) {
        System.err.println(e.getMessage());
        e.printStackTrace();

    }

在某些情况下,在 "continue entries;" 之后尝试下一个循环,我看到 "stream close Exception" :/

error:Stream closed

堆栈跟踪:

java.io.IOException: Stream closed
at java.util.zip.ZipInputStream.ensureOpen(ZipInputStream.java:67)
at java.util.zip.ZipInputStream.getNextEntry(ZipInputStream.java:116)
at org.parsisys.test.mina.view.SimpleFtplet$beaVersion0_1155.isTransferFinished(SimpleFtplet.java:299)
at org.parsisys.test.mina.view.SimpleFtplet.isTransferFinished(SimpleFtplet.java)
at org.parsisys.test.mina.view.SimpleFtplet.beaAccessisTransferFinished(SimpleFtplet.java)
at org.parsisys.test.mina.view.SimpleFtplet$beaVersion0_1155.onUploadEnd(SimpleFtplet.java:208)
at org.parsisys.test.mina.view.SimpleFtplet.onUploadEnd(SimpleFtplet.java)
at org.apache.ftpserver.ftplet.DefaultFtplet.afterCommand(DefaultFtplet.java:89)
at org.parsisys.test.mina.view.SimpleFtplet.afterCommand(SimpleFtplet.java)
at org.apache.ftpserver.ftpletcontainer.impl.DefaultFtpletContainer.afterCommand(DefaultFtpletContainer.java:144)
at org.apache.ftpserver.impl.DefaultFtpHandler.messageReceived(DefaultFtpHandler.java:220)
at org.apache.ftpserver.listener.nio.FtpHandlerAdapter.messageReceived(FtpHandlerAdapter.java:61)
at org.apache.mina.core.filterchain.DefaultIoFilterChain$TailFilter.messageReceived(DefaultIoFilterChain.java:716)
at org.apache.mina.core.filterchain.DefaultIoFilterChain.callNextMessageReceived(DefaultIoFilterChain.java:434)
at org.apache.mina.core.filterchain.DefaultIoFilterChain.access00(DefaultIoFilterChain.java:46)
at org.apache.mina.core.filterchain.DefaultIoFilterChain$EntryImpl.messageReceived(DefaultIoFilterChain.java:796)
at org.apache.ftpserver.listener.nio.FtpLoggingFilter.messageReceived(FtpLoggingFilter.java:85)
at org.apache.mina.core.filterchain.DefaultIoFilterChain.callNextMessageReceived(DefaultIoFilterChain.java:434)
at org.apache.mina.core.filterchain.DefaultIoFilterChain.access00(DefaultIoFilterChain.java:46)
at org.apache.mina.core.filterchain.DefaultIoFilterChain$EntryImpl.messageReceived(DefaultIoFilterChain.java:796)
at org.apache.mina.core.filterchain.IoFilterEvent.fire(IoFilterEvent.java:75)
at org.apache.mina.filter.logging.MdcInjectionFilter.filter(MdcInjectionFilter.java:136)
at org.apache.mina.filter.util.CommonEventFilter.messageReceived(CommonEventFilter.java:70)
at org.apache.mina.core.filterchain.DefaultIoFilterChain.callNextMessageReceived(DefaultIoFilterChain.java:434)
at org.apache.mina.core.filterchain.DefaultIoFilterChain.access00(DefaultIoFilterChain.java:46)
at org.apache.mina.core.filterchain.DefaultIoFilterChain$EntryImpl.messageReceived(DefaultIoFilterChain.java:796)
at org.apache.mina.filter.codec.ProtocolCodecFilter$ProtocolDecoderOutputImpl.flush(ProtocolCodecFilter.java:427)
at org.apache.mina.filter.codec.ProtocolCodecFilter.messageReceived(ProtocolCodecFilter.java:245)
at org.apache.mina.core.filterchain.DefaultIoFilterChain.callNextMessageReceived(DefaultIoFilterChain.java:434)
at org.apache.mina.core.filterchain.DefaultIoFilterChain.access00(DefaultIoFilterChain.java:46)
at org.apache.mina.core.filterchain.DefaultIoFilterChain$EntryImpl.messageReceived(DefaultIoFilterChain.java:796)
at org.apache.mina.core.filterchain.IoFilterEvent.fire(IoFilterEvent.java:75)
at org.apache.mina.core.session.IoEvent.run(IoEvent.java:63)
at org.apache.mina.filter.executor.OrderedThreadPoolExecutor$Worker.runTask(OrderedThreadPoolExecutor.java:780)
at org.apache.mina.filter.executor.OrderedThreadPoolExecutor$Worker.runTasks(OrderedThreadPoolExecutor.java:772)
at org.apache.mina.filter.executor.OrderedThreadPoolExecutor$Worker.run(OrderedThreadPoolExecutor.java:714)
at java.lang.Thread.run(Thread.java:748)

请帮帮我................................................ ..................................................... ..................................................... ..................................................... ..................................................... ..

您的最外层 BufferedReader 对象似乎关闭了您的嵌套流(特别是 ZipInputStream)。尝试将 BufferedReader 初始化代码移到循环逻辑的更高层。

此主题也可能有帮助:closing nested streams

更新: 好的,现在一切都清楚了。 DOMParser class 的实现代码清楚地表明 parse 方法关闭了底层 InputStream 源代码(只是 finally 块的摘录):

} finally {
    this.parser.reader.close();
}

在这种情况下可以做的是破解传递给 DOMParser 对象的 BufferedReader。这是一个例子:

public class HackedReader extends BufferedReader {

    public HackedReader(InputStreamReader inputStreamReader) {
        super(inputStreamReader);
    }

    @Override
    public void close() {
        // Close method doesn't do anything, that's the main sense of overriding.
    }

    // But you know exact method which will close your underlying stream.
    public void hackedClose() throws IOException {
        super.close();
    }
}

我发现使用 org.apache.poi.util.CloseIgnoringInputStream 对我有用。我能够将传递给另一个方法的 ZipInputStream 包装起来。

例如:

ExcelUtility.getLineCount(new CloseIgnoringInputStream(zipStream)

如果你能实现一个逻辑来理解是否没有更多的“路线”要读取,在 If 块的末尾你可以插入一个 break 指令来退出 while 块并避免尝试读取关闭的流