在調(diào)用go語言編寫的server進(jìn)行下載文件時(shí)出現(xiàn)
java.net.ProtocolException: unexpected end of stream
at com.squareup.okhttp.internal.http.HttpConnection$FixedLengthSource.read(HttpConnection.java:421)
java.io.IOException: unexpected end of stream
at com.squareup.okhttp.internal.http.HttpConnection$ChunkedSource.read(HttpConnection.java:467)
原因:
go server響應(yīng)時(shí)的邏輯是
n, err := io.Copy(w, f)
而go http 的底層tcp連接設(shè)置了(timeout)超時(shí)時(shí)間,由于文件過大或者并發(fā)過大,導(dǎo)致流沒有復(fù)制完,所以出現(xiàn)上述報(bào)錯(cuò)
出現(xiàn)第一種報(bào)錯(cuò)是因?yàn)?,?code>go server端在上述代碼前設(shè)置了content-length 而因?yàn)橛?code>content-length,所以調(diào)用read的具體方法不同
參見http://stackoverflow.com/questions/26096944/when-responding-using-io-copy-who-should-be-responsible-for-the-error