【问题标题】:Why Image is not showing on the server when uploaded with Retrofit Multipart?为什么使用 Retrofit Multipart 上传时图像没有显示在服务器上?
【发布时间】:2020-02-18 17:44:08
【问题描述】:

我正在使用 Retrofit Multipart 将图像上传到服务器上。

当我发送请求时,它正在创建一个图像以及传递的名称和大小。

但是当我尝试在浏览器中打开该图像时,它说图像包含错误。

当我尝试向邮递员发送请求时,

发生了同样的情况。但是我发现我发送了错误的参数。所以我将Content-Type 更改为application/binary 并将图片发布在 Body-> binary成功了

所以我怀疑我没有通过应用程序发送正确的Content-Type。但是我查了一下,什么也没发现

来自ApiInterface.java

@Multipart
@POST("wp-json/wp/v2/media/")
Call<ImagePostResult> uploadImage(@Header("Authorization") String authHeader,
                                  @Header("Content-Type") String contentType,
                                  @Header("Content-Disposition") String contentDisposition,
                                  @Part MultipartBody.Part file,
                                  @Part("description") RequestBody description);

我的要求:

private void uploadIdentityProofImageToServer() {
    progressDialog.setMessage("Uploading identity proof");

    ApiInterface apiInterface = getApiInterfaceObj();

    // For BasicAuth
    String authHeader = getAuthHeader();

    //File creating from selected URL
    String imageLocalPath = orderDetailsArrayList.get(getAdapterPosition()).getCapturedPhotoPath();//encoded image
    Uri tempUri = orderDetailsArrayList.get(getAdapterPosition()).getTempUri();// uri of image

    File file = new File(imageLocalPath);

    // create RequestBody instance from file
    RequestBody requestBodyFile = RequestBody.create(MediaType.parse(mContext.getContentResolver().getType(tempUri)), file);

    String fileName = file.getName();
    MultipartBody.Part body = MultipartBody.Part.createFormData("picture", fileName, requestBodyFile);

    String descriptionString = "Sample description";
    RequestBody description = RequestBody.create(MediaType.parse("multipart/form-data"), descriptionString);

    String contentType = "application/binary";
    String contentDisposition = "attachment; filename =  " + fileName;


    Call<ImagePostResult> resultCall = apiInterface.uploadImage(
            authHeader,
            contentType,
            contentDisposition,
            body,
            description);

    resultCall.enqueue(new Callback<ImagePostResult>() {
        @Override
        public void onResponse(Call<ImagePostResult> call, Response<ImagePostResult> response) {
            // Response Success
            if (response.isSuccessful()) {
                ImagePostResult imagePostResult = response.body();
                String raw = imagePostResult.getGuid().getRaw();

                updateThePathOfImage(raw);
            }
            Log.d(TAG, "onResponse: " + response.message());
        }

        @Override
        public void onFailure(Call<ImagePostResult> call, Throwable t) {
            Log.d(TAG, "onFailure: " + t);
            progressDialog.dismiss();
            Toast.makeText(mContext, "" + t.getMessage(), Toast.LENGTH_SHORT).show();
        }
    });
}

另外,如果您可以链接任何资源以上传二进制格式的图像,那将很有帮助。

【问题讨论】:

    标签: android retrofit multipartform-data


    【解决方案1】:

    找到了

    我遵循this issue末尾给出的解决方案代码

    这是我的小修改代码

    @POST("wp-json/wp/v2/media/")
    Call<ImagePostResult> postEventPhoto(
            @Header("Authorization") String accessToken,
            @Header("Content-Type") String contentType,
            @Header("Content-Disposition") String contentDisposition,
            @Body RequestBody photo);
    

    这是请求

            // For BasicAuth
            String authHeader = getAuthHeader();
    
            String contentType = "application/binary";
            String contentDisposition = "attachment; filename =  " + fileName;
    
            RequestBody requestBodyee = null;
            try {
                InputStream in = new FileInputStream(file);
    
                byte[] buf;
                buf = new byte[in.available()];
                while (in.read(buf) != -1) ;
                requestBodyee = RequestBody
                        .create(MediaType.parse("application/octet-stream"), buf);
            } catch (IOException e) {
                e.printStackTrace();
            }
    
    
            Call<ImagePostResult> imagePostResultCall = apiInterface.postEventPhoto(
                    authHeader,
                    contentType,
                    contentDisposition,
                    requestBodyee);
            imagePostResultCall.enqueue(new Callback<ImagePostResult>() {
                @Override
                public void onResponse(Call<ImagePostResult> call, Response<ImagePostResult> response) {
                    // Response Success
                    if (response.isSuccessful()) {
                      // yaay
                    }
                }
    
                @Override
                public void onFailure(Call<ImagePostResult> call, Throwable t) {
                    Log.d(TAG, "onFailure: " + t);
                }
            });
    

    【讨论】:

      猜你喜欢
      • 2019-08-10
      • 1970-01-01
      • 2017-09-28
      • 2021-03-18
      • 2016-04-25
      • 1970-01-01
      • 1970-01-01
      • 2017-07-14
      相关资源
      最近更新 更多