简体   繁体   English

java.net.ProtocolException:当我通过okhttp上传图像到服务器时,发生了意外的流结束

[英]java.net.ProtocolException: unexpected end of stream happened when i uploaded a image to server by okhttp

The error happened when i upload a 115KB image file to server.(the most answer of stackoverflow is about download the file.I do not know if it is the same to those) the error information is below: onFailure : java.net.ProtocolException: unexpected end of stream 当我将一个115KB的图像文件上传到服务器时发生了错误。(stackoverflow的最大答案是关于下载文件。我不知道它们是否相同),错误信息如下:onFailure:java.net.ProtocolException :流的意外结束

Relevant Code: 相关代码:

public void upLoadImageFile(String uploadUrl, File file, Map<String, String> maps, final HWUploadListener listener) {


    final CallbackHandler handler = new CallbackHandler(listener);

    try {
        MultipartBody.Builder builder = new MultipartBody.Builder().setType(MultipartBody.FORM);
        if (maps == null) {
            builder.addPart(Headers.of("Content-Disposition", "form-data; name=\"image\";filename=\"file.jpg\""),
                    RequestBody.create(MediaType.parse("image/jpeg"), file)).build();

        } else {
            for (String key : maps.keySet()) {
                builder.addFormDataPart(key, maps.get(key));
            }
            builder.addPart(Headers.of("Content-Disposition", "form-data; name=\"image\";filename=" + file.getName()), RequestBody.create(MediaType.parse("image/jpeg"), file)
            );
        }


        RequestBody body = builder.build();

        final Request request = new Request.Builder().url(uploadUrl).post(body).build();

        final Call call = mOkHttpClient.newBuilder().writeTimeout(50, TimeUnit.SECONDS).build().newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                UtilUtils1.log("HuowuSdk", "onFailure :" + e.toString());
                handler.uploadFailure(e.toString());
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {

                if (response.isSuccessful()) {
                    String result = response.body().string();
                    handler.uploadSuccess(result);

                } else {
                    handler.uploadFailure(response.message());
                }
            }
        });

    } catch (Exception e) {
        UtilUtils1.log("HuowuSdk", e.toString());
        handler.uploadError(e.toString());
    }
}

Appreciate your answer!! 感谢您的回答!

在下面的此行中,您必须增加写超时,因为在上载时,您的写超时将过期,因此可能是在下面的行中增加writeTimeout限制的原因:

final Call call = mOkHttpClient.newBuilder().writeTimeout(50, TimeUnit.SECONDS).build().newCall(request);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Okhttp java.net.ProtocolException:stream 意外结束 - Okhttp java.net.ProtocolException: unexpected end of stream java.net.ProtocolException:流的意外结束,在慢速网络上AsyncTask文件下载失败 - java.net.ProtocolException: unexpected end of stream, AsyncTask File Download failed on slow networks OkHttp“java.net.ProtocolException:意外状态行:nullHTTP / 1.1 200 OK”在使用相同连接的“204 NO-CONTENT”响应之后 - OkHttp “java.net.ProtocolException: Unexpected status line: nullHTTP/1.1 200 OK” after a “204 NO-CONTENT” response using same connection 改造“java.net.ProtocolException:意外的状态行”,任何人? - Retrofit “java.net.ProtocolException: Unexpected status line”, Anyone? java.net.ProtocolException: 服务器重定向次数过多 (20) - java.net.ProtocolException: Server redirected too many times (20) java.net.ProtocolException: 服务器重定向次数过多 - java.net.ProtocolException: Server redirected too many times ProtocolException:意外结束流 - ProtocolException: unexpected end of stream android中带有HttpsURLConnection的java.net.protocolException - java.net.protocolException with HttpsURLConnection in android java.net.ProtocolException:意外的状态行: <!DOCTYPE HTML PUBLIC “-//IETF//DTD HTML 2.0//EN”> - java.net.ProtocolException: Unexpected status line: <!DOCTYPE HTML PUBLIC “-//IETF//DTD HTML 2.0//EN”> java.net.Authenticator:java.net.ProtocolException:服务器重定向太多次(20) - java.net.Authenticator : java.net.ProtocolException: Server redirected too many times (20)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM