ProtocolException: unexpected end of stream while downloading a file
See original GitHub issueUsing a library which uses okhttp
to download files, I am usually getting an unexpected end of stream
error while downloading large files (thrown at the end of the download). I am using the following code and URL to reproduce the error:
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder().url("http://speedtest-sfo1.digitalocean.com/100mb.test").build();
client.newCall(request).enqueue(new Callback() {
public void onFailure(Call call, IOException e) {
e.printStackTrace();
}
public void onResponse(Call call, Response response) throws IOException {
if (!response.isSuccessful()) {
throw new IOException("Failed to download file: " + response);
}
FileOutputStream fos = new FileOutputStream(downloadDirectory + File.separator + "100mb.test");
fos.write(response.body().bytes());
fos.close();
}
});
URL: http://speedtest-sfo1.digitalocean.com/100mb.test (This is not the only URL the error is thrown on)
The code has been tested on API 22, 25, 26, 27. The error is not thrown always, I tested it 10 times and got the error 6 times, 4 times the file got successfully downloaded and saved…
I have read about the error and I know that it gets thrown when the servers sends incorrect Content-Length
, not this time though. The file is exactly 100MB and the server supplies its correct length. I am not sending any extra request headers, the response headers are:
Server => nginx/1.13.3
Date => Sat, 09 Dec 2017 17:19:31 GMT
Content-Type => text/plain
Content-Length => 104857600
Last-Modified => Mon, 27 Nov 2017 09:11:00 GMT
Connection => close
ETag => "5a1bd6a4-6400000"
Access-Control-Allow-Origin => *
Access-Control-Allow-Headers => Origin,X-RequestedWith,Content-Type,Range
Access-Control-Allow-Methods => GET, OPTIONS
Accept-Ranges => bytes
Cache-Control => max-age=0, no-cache, no-store, mustrevalidate, no-transform
The complete error:
Callback failure for call to http://speedtest-sfo1.digitalocean.com/...
java.net.ProtocolException: unexpected end of stream
at okhttp3.internal.http1.Http1Codec$FixedLengthSource.read(Http1Codec.java:406)
at okio.Buffer.writeAll(Buffer.java:1005)
at okio.RealBufferedSource.readByteArray(RealBufferedSource.java:107)
at okhttp3.ResponseBody.bytes(ResponseBody.java:136)
at dm.com.downloadmanager.MainActivity$3.onResponse(MainActivity.java:273)
at okhttp3.RealCall$AsyncCall.execute(RealCall.java:153)
at okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
at java.lang.Thread.run(Thread.java:764)
Issue Analytics
- State:
- Created 6 years ago
- Comments:48 (6 by maintainers)
Top GitHub Comments
hello ,I fix this by changing Apache configuration. just like the picture shows below.
In a specific series of models + a specific server + a specific Wifi network, the probability of this problem is very high, and it is also related to the network usage at the time. The problem I encountered is that the probability of occurrence is very high in Huawei mobile phones, Alibaba Cloud CDN and my company’s Wifi. After verification, it is found that the default value of the ReceiveBufferSize of the socket in the Huawei system is 2097152, and the default value of the Samsung system is 1048576. The guess is related to the system tuning of this parameter. Adjust this parameter to below 49152, the probability of this problem is very low, and it is almost difficult to reproduce. You can also try if you encounter similar problems. Core code: new OkHttpClient.Builder().socketFactory(), socket.setReceiveBufferSize(49152);