Question

My question is regarding this exception

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)

So we recently upgraded our code for Java 7, and we had to implement a try block for a ZipFile declaration. This seems to cause the input stream to close, where it didn't before when there was no try block. Not sure I understand why, when it is declared before the block. Can anyone explain or offer a solution?

    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();
    }
Était-ce utile?

La solution

In the current code you are using the try-with-resources statement, so your ZipFile docsZip is closed as soon as the try is completed.

When you put the declaration of docsZip within the try it will not be closed (note that this should then be done somewhere after out.close().

Autres conseils

This is documented behavior in ZipFile#close:

Closing this ZIP file will close all of the input streams previously returned by invocations of the getInputStream method.

(A Java 7 try-with-resources statement calls close automatically at the end of the block.)

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top