java.io.EOFException:ZLIB 输入流意外结束 - 从 HTTP 读取

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6778546/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 17:14:29  来源:igfitidea点击:

java.io.EOFException: Unexpected end of ZLIB input stream - reading from HTTP

javahttpzlib

提问by tzafrir

I tried looking around for similar problem but couldn't find any solution that resemble my problem:

我尝试四处寻找类似的问题,但找不到与我的问题类似的任何解决方案:

I use the following piece of code to read from HttpUrlConnection:

我使用以下代码从 HttpUrlConnection 读取:

public static BufferedReader getConnectionReader(HttpURLConnection con, String url)
        throws Exception {
    con = (HttpURLConnection) new URL(url).openConnection();
    con.connect();
    if (cm != null) {
        cm.storeCookies(con);
    }
    if (con.getHeaderField("Content-Encoding") != null
            && con.getHeaderField("Content-Encoding").equalsIgnoreCase("gzip")) {
        return new BufferedReader(new InputStreamReader(new GZIPInputStream(con.getInputStream())));
    } else
        return new BufferedReader(new InputStreamReader(con.getInputStream()));
}

Reading is performed in the following way:

读取通过以下方式进行:

HttpURLConnection con = null;
reader = Utils.getConnectionReader(con, "http://www.site.com/page.html");
String line = null;
while ((line = reader.readLine()) != null) {
    log.info(line);
}

Sometimes I get the mentioned exception:

有时我会收到提到的异常:

java.io.EOFException: Unexpected end of ZLIB input stream

java.io.EOFException:ZLIB 输入流意外结束

When I can, I catch this exception and retry the operation - successfully.

如果可以,我会捕获此异常并重试该操作 - 成功。

The problem is that I don't know what is causing this exception to pop. It happens quite randomly.

问题是我不知道是什么导致了这个异常弹出。它发生得很随机。

I want to believe it is a network issue.

我想相信这是一个网络问题。

Anyone found a way to fully solve such problem?

有没有人找到完全解决此类问题的方法?

Thanks!!

谢谢!!

采纳答案by Perception

The method your using is less than ideal for binary formats like GZip, I assume you are doing that just for testing? Apart from that and the bug with the HTTPURLConnection, there's not much else in code that could be causing the issue. I would recommend reading using byte buffer swapping, at least to eliminate that as a possible source of error.

您使用的方法对于 GZip 等二进制格式不太理想,我假设您这样做只是为了测试?除了这个和 HTTPURLConnection 的错误之外,代码中没有其他可能导致问题的地方。我建议使用字节缓冲区交换来阅读,至少可以消除它作为可能的错误来源。