尝试阻止导致流关闭IOException [英] Try block causing Stream closed IOException

查看:89
本文介绍了尝试阻止导致流关闭IOException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是关于这种例外情况

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)

因此,我们最近升级了Java 7的代码,并且必须为ZipFile声明实现try块.这似乎导致输入流关闭,在没有try块的情况下它没有关闭.不知道为什么,为什么在块之前声明它.谁能解释或提供解决方案?

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

推荐答案

在当前代码中,您正在使用

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.

docsZip的声明放在try中时,它不会关闭(请注意,然后应在out.close()之后的某个地方进行此操作.

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().

这篇关于尝试阻止导致流关闭IOException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆