Java Try块导致流关闭IOException

Java Try块导致流关闭IOException,java,try-catch,inputstream,Java,Try Catch,Inputstream,我的问题是关于这个例外 java.io.IOException: Stream closed java.util.zip.InflaterInputStream.ensureOpen(InflaterInputStream.java:67) java.util.zip.InflaterInputStream.read(InflaterInputStream.java:142) java.io.FilterInputStream.read(FilterInputStream.java:107) c

我的问题是关于这个例外

java.io.IOException: Stream closed
java.util.zip.InflaterInputStream.ensureOpen(InflaterInputStream.java:67)
java.util.zip.InflaterInputStream.read(InflaterInputStream.java:142)
java.io.FilterInputStream.read(FilterInputStream.java:107)
com.avnet.css.servlet.PartNotificationFileServlet.doGet(PartNotificationFileServlet.java:51)
javax.servlet.http.HttpServlet.service(HttpServlet.java:734)
javax.servlet.http.HttpServlet.service(HttpServlet.java:847)
因此,我们最近升级了Java 7的代码,我们必须为ZipFile声明实现一个try块。这似乎会导致输入流关闭,而在没有try块的情况下,它以前没有关闭。我不知道为什么,当它在块前声明时。有人能解释或提供解决方案吗

    String uri = req.getRequestURI();
    String[] tokens = uri.split("/");
    String folder = tokens[tokens.length - 2];//req.getParameter("folder");
    String filename = tokens[tokens.length - 1];req.getParameter("filename");
    Properties properties = new CSSProperties();
    InputStream inputStream = null;
    if(!folder.contains("_AVT")){
        //below is the new try block, when the declaration inside the parenthesis is on its own, instead of inside try, it works fine.  
        try (ZipFile docsZip = new ZipFile(new File(properties.getProperty(PCN_DIR) + File.separator + folder + File.separator + "Documents.zip"))) {
            ZipEntry entry = docsZip.getEntry(filename);
            if (entry != null)                              
                inputStream = docsZip.getInputStream(entry);
        }
    }
    else{
        String decodedFileName = URLDecoder.decode(filename, "UTF-8");
        File file = new File(properties.getProperty(PCN_DIR) + File.separator + folder + File.separator + decodedFileName);
        if(file.exists())
            inputStream = new FileInputStream(file);
    }
    if(inputStream != null){
        resp.setHeader("Content-Type", MimetypesFileTypeMap.getDefaultFileTypeMap().getContentType(filename));
        ServletOutputStream out = resp.getOutputStream();
        byte[] buffer = new byte[2048];
        int read;
        while ((read = inputStream.read(buffer)) > 0) //this is line 51, where the error occurs
            out.write(buffer, 0, read);
        inputStream.close();
        out.close();
    }

这是在以下文件中记录的行为:

关闭此ZIP文件将关闭之前通过调用getInputStream方法返回的所有输入流


(Java 7语句在块的末尾自动调用
close

这一行为记录在:

关闭此ZIP文件将关闭之前通过调用getInputStream方法返回的所有输入流


(Java 7语句在块的末尾自动调用
close

这一行为记录在:

关闭此ZIP文件将关闭之前通过调用getInputStream方法返回的所有输入流


(Java 7语句在块的末尾自动调用
close

这一行为记录在:

关闭此ZIP文件将关闭之前通过调用getInputStream方法返回的所有输入流


(Java 7语句在块的末尾自动调用
close

在当前代码中,您正在使用,因此在
try
完成后,您的
ZipFile docsZip
就会关闭


当您将
docsZip
的声明放入
try
中时,它将不会关闭(请注意,这应该在当前代码中的
out.close()
之后的某个位置执行),因此您的
ZipFile docsZip
将在
try
完成后立即关闭


当您将
docsZip
的声明放入
try
中时,它将不会关闭(请注意,这应该在当前代码中的
out.close()
之后的某个位置执行),因此您的
ZipFile docsZip
将在
try
完成后立即关闭


当您将
docsZip
的声明放入
try
中时,它将不会关闭(请注意,这应该在当前代码中的
out.close()
之后的某个位置执行),因此您的
ZipFile docsZip
将在
try
完成后立即关闭


当您将
docsZip
的声明放在
try
中时,它将不会关闭(请注意,这应该在
out.close()之后的某个位置执行)

代码中的try-catch块在哪里?代码中的try-catch块在哪里?代码中的try-catch块在哪里?因此,如果我只是将其从该行中删除,将其扔到try块中,并有一个空的finally块,一切都会好起来?这可能会防止您当前遇到的异常注意,使用空的finally块可能会导致其他问题,因为
docsZip
资源从未关闭过。很酷,谢谢你的解释。我想我现在已经把它整理好了。所以如果我只是从那行中删除它,把它扔到try块中,并拥有一个空的finally块,一切都会好的?这可能会阻止异常您当前遇到的问题。请注意,使用空的finally块可能会导致其他问题,因为
docsZip
资源从未关闭过。很酷,谢谢您的解释。我想我现在已经解决了。因此,如果我只是将其从该行中删除,将其扔到try块中,并拥有一个空的finally块,一切都会好的?会好的可能会阻止您当前遇到的异常。请注意,使用空的finally块可能会导致其他问题,因为
docsZip
资源从未关闭过。很酷,感谢您的解释。我想我现在已经解决了。因此,如果我只是将其从该行中删除,请将其扔到try块中,并拥有一个空的finally块,一切都会好的?这可能会阻止您当前遇到的异常。请注意,使用空的finally块可能会导致其他问题,因为
docsZip
资源从未关闭过。很酷,感谢您的解释。我想我现在已经解决了。