发送带有改版的多部分POST请求的大文件时出错

时间:2018-09-16 10:58:35

标签: java android retrofit2

我正在使用Retrofit2库将数据发布到服务器,我需要使用POST请求上传文件或照片,并获取它的响应。所以我有两种方法:

  1. 将文件编码为base64,然后在正文中以文本形式发送。
  2. 使用多部分请求并直接发送文件。

我尝试了它们,但是在两种方式下,我都有相同的问题:
当我发送小文件(例如500字节以下)时,我会获得成功的结果。
但是,当我发送大文件时,就会出现问题。在超时结束之前,我什么也没收到,在AVD中我获得了成功的响应(在Postman中也是如此),但是在实际设备(带有Android 5.1的HTC和带有Android 7.0的Samsung S6)中,发生以下错误:

java.io.IOException: unexpected end of stream on Connection{myIp:port, proxy=DIRECT@ hostAddress=/myIp:port cipherSuite=none protocol=http/1.1

调用方法:

@Multipart
@POST("uploaddata")
Call<ResponseBody> uploaddate(@Part("file") RequestBody filePart, @Part("token") RequestBody token,
                              @Part("type") RequestBody type, @Part("name")RequestBody name);

主要方法:

public void uploadImage(String type, File file, String name, String token, final DataInterface listener) {

    RequestBody filePart = RequestBody.create(MediaType.parse("image/*"), file);
    RequestBody tokenPart = RequestBody.create(MediaType.parse("text/plain"), token);
    RequestBody namePart = RequestBody.create(MediaType.parse("text/plain"), name);
    RequestBody typePart = RequestBody.create(MediaType.parse("text/plain"), type);

    Call<ResponseBody> uploadData = apiInterface.uploaddate(filePart, tokenPart, typePart, namePart);
    uploadData.enqueue(new Callback<ResponseBody>() {
        @Override
        public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
            if (response.isSuccessful()) {

                listener.onResponseListener(response.body());

            } else {

                Log.e(TAG, "uploadData -----> response.isSuccessful == false");

            }
        }

        @Override
        public void onFailure(Call<ResponseBody> call, Throwable t) {
            Log.e(TAG, "uploadData onFailure -----> " + t);
        }
    });
}

1 个答案:

答案 0 :(得分:0)

您能否发布错误的日志输出 它是否返回了像大型二进制数据之类的东西?

尝试一下:

@Multipart
@POST("uploaddata")
Call<ResponseBody> uploaddate(@Part MultiPart.Body filePart, 
@Part("token") RequestBody token,
                          @Part("type") RequestBody type, 
@Part("name")RequestBody name);

主要方法:

public void uploadImage(String type, File file, String name, String token, 
final DataInterface listener) {

RequestBody filePart = RequestBody.create(MediaType.parse("image/*"), file);
MultiPartBody.Part body = MultiPartBody.Part.createFormData("filePart",file.getName(),filePart);
RequestBody tokenPart = RequestBody.create(MediaType.parse("text/plain"), token);
RequestBody namePart = RequestBody.create(MediaType.parse("text/plain"), name);
RequestBody typePart = RequestBody.create(MediaType.parse("text/plain"), type);

Call<ResponseBody> uploadData = apiInterface.uploaddate(body, tokenPart, typePart, namePart);
uploadData.enqueue(new Callback<ResponseBody>() {
    @Override
    public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
        if (response.isSuccessful()) {

            listener.onResponseListener(response.body());

        } else {

            Log.e(TAG, "uploadData -----> response.isSuccessful == false");

        }
    }

    @Override
    public void onFailure(Call<ResponseBody> call, Throwable t) {
        Log.e(TAG, "uploadData onFailure -----> " + t);
    }
});

}