java.io.IOException:流意外结束

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35080981/
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-11-02 23:42:49  来源:igfitidea点击:

java.io.IOException: unexpected end of stream

javaandroidhttpurlconnectionmultipartform-data

提问by JosephM

getting issue when writing large video file using httpurlconnection.

使用 httpurlconnection 写入大型视频文件时出现问题。

java.io.IOException: unexpected end of stream on Connection{192.1.4.55, proxy=DIRECT@ hostAddress=192.1.4.55 cipherSuite=none protocol=http/1.1} (recycle count=0)
W/System.err:     at com.android.okhttp.internal.http.HttpConnection.readResponse(HttpConnection.java:210)W/System.err:     at com.android.okhttp.internal.http.HttpTransport.readResponseHeaders(HttpTransport.java:80)
W/System.err:     at com.android.okhttp.internal.http.HttpEngine.readNetworkResponse(HttpEngine.java:904)
W/System.err:     at com.android.okhttp.internal.http.HttpEngine.readResponse(HttpEngine.java:788)
at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:443)
at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:388)
at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:501)

response code is here

响应代码在这里

final InputStream is = connection.getInputStream();
            final ByteArrayOutputStream bytes = new ByteArrayOutputStream();
            final byte[] buffer = new byte[maxBufferSize];
            int bytesRead;
            while ((bytesRead = is.read(buffer, 0, 1024)) != -1) {
                bytes.write(buffer, 0, bytesRead);
            }
            log.log(INFO,
                    format("{0} took {4} ms", url,
                            (currentTimeMillis() - start)));
            String response = new String(bytes.toByteArray());

回答by sajid45

Check it if you are using OkHttpClient, here is a parameter retryOnConnectionFailure(false). By default it is false, you just make it true then error will be removed. Hopefully because I have same problem and solve just change it.

如果您使用的是 OkHttpClient,请检查它,这里是一个参数 retryOnConnectionFailure(false)。默认情况下它是假的,您只需将其设为真,然后错误将被删除。希望因为我有同样的问题并解决了改变它。

OkHttpClient client = new OkHttpClient.Builder()
    .retryOnConnectionFailure(true)
    .build();

回答by Harisudan Kuppusami

Please use BufferedInputStream and BufferedOutputStream to buffer the data for larger files

请使用 BufferedInputStream 和 BufferedOutputStream 来缓冲较大文件的数据

回答by Abhishek

Hope this Code will Help you .

希望此代码对您有所帮助。

BufferedReader br = null;
if (conn.getResponseCode() == 200) {
    Log.e("HTTP success code", "" + conn.getResponseCode());
    InputStream inputStream = conn.getInputStream();
    if (inputStream != null)
        br = new BufferedReader(new InputStreamReader(inputStream));
} else {
    Log.e("HTTP error code", "" + conn.getResponseCode());
    InputStream inputStream = conn.getErrorStream();
    if (inputStream != null)
        br = new BufferedReader(new InputStreamReader(inputStream));
} 
StringBuilder builder = new StringBuilder();
String output;
while ((output = br.readLine()) != null) {
    builder.append(output);
}
Log.e(reqTag + " Response :", builder.toString());
conn.disconnect();

回答by zzzmode

Sometimes this exception can be ignore,you may need to retry.
e.g. use charles proxy you mobile phone wifi,download file,charles Terminate this connection,you will receive same exception.

有时可以忽略此异常,您可能需要重试。
例如使用查尔斯代理您的手机wifi,下载文件,查尔斯终止此连接,您将收到相同的异常。