为什么使用 Retrofit Multipart 上传时图像没有显示在服务器上?

Why Image is not showing on the server when uploaded with Retrofit Multipart?

我正在使用 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();
        }
    });
}

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

找到了

我遵循了 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);
            }
        });