简体   繁体   English

如何使用Retrofit2 multipart上传图像?

[英]How to upload Image using retrofit2 multipart?

I totally got struct in uploading an image on server. 我完全可以在服务器上上传图像。 I'm getting response from server successfully but image is not getting upload on server. 我已成功从服务器获得响应,但是图像未在服务器上上传。 Always asking for "Insert Your Image". 始终要求“插入您的图像”。

here is my code please check this, where i'm making mistake 这是我的代码,请检查一下,我在哪里出错

private void uploadFile(Uri fileUri) {
    File file = new File(fileUri.getPath());
    if (file.exists()) {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(BASE_URL)
                .addConverterFactory(ScalarsConverterFactory.create())
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        Log.e(TAG, "uploadFile:File "+file );
        MediaType MEDIA_TYPE_PNG = MediaType.parse("multipart/form-data");

        APIInterface service = retrofit.create(APIInterface.class);

        RequestBody requestFile = RequestBody.create(MEDIA_TYPE_PNG, file);
        Log.e(TAG, "uploadUri: " + strEmpsno + " " + strStoreSno + "  " + strLrno + "  " + strRecqty + "  " + strDeliverydate + " " + strDeliverytime);
        Call<PODResponse> call = service.postFile(strEmpsno, strStoreSno, strLrno, strRecqty, strRecvol,
                strRecwgt, strDamageqty, strLooseqty, strDeliverydate
                , strDeliverytime, requestFile, strRemarks, strReceivedby,
                strIpaddress);

        call.enqueue(new Callback<PODResponse>() {
            @Override
            public void onResponse(Response<PODResponse> response) {
                Log.e(TAG, "onResponse:uploadUri " + response.isSuccess());
                if (response.isSuccess()) {
                    Log.e(TAG, "uploadUri: " + response.body().getResult());
                }
            }

            @Override
            public void onFailure(Throwable t) {
                Log.e(TAG, "onFailure:uploadUri " + t.getLocalizedMessage());
            }
        });
    }
  }

In log of Log.e(TAG, "uploadFile:File "+file ); Log.e(TAG, "uploadFile:File "+file );日志中Log.e(TAG, "uploadFile:File "+file ); i'm getting 我越来越

/storage/emulated/0/Audex/saved_images/logo.jpg /storage/emulated/0/Audex/saved_images/logo.jpg

This is my Image name with path . 这是我的Image name with path From Rest(chrome extension) image is successfully getting stored on server but not by using this code. Rest(chrome extension)图像已成功存储在服务器上,但没有使用此代码。 Please help me. 请帮我。

My Interface is like 我的界面就像

  @Multipart
@POST("/savePic")
Call<PODResponse> postFile(
        @Part("empsno") String empsno,
        @Part("storesno") String storesno,
        @Part("lrSno") String lrSno,
        @Part("recQty") String recQty,
        @Part("recVol") String recVol,
        @Part("recWgt") String recWgt,
        @Part("damageQty") String damageQty,
        @Part("looseQty") String looseQty,
        @Part("deliveryDate") String deliveryDate,
        @Part("deliveryTime") String deliveryTime,
        @Part("uploadFile\"; filename=\"audex.jpg\" ") RequestBody part,
        @Part("remarks") String remarks,
        @Part("receivedBy") String receivedBy,
        @Part("ipAddress") String ipAddress
);

Thank you All in advance 谢谢大家

After long long time i got my answer. 经过很长一段时间,我得到了答案。 Thanks all who tried for me. 谢谢所有为我努力的人。 Only error was i was putting RequestBody instead of String. 唯一的错误是我放了RequestBody而不是String。

  @Multipart
  @POST("/savePic")
  Call<PODResponse> postFile(
    @Part("empsno") String empsno,
    @Part("storesno") String storesno,
    @Part("lrSno") String lrSno,
    @Part("recQty") String recQty,
    @Part("recVol") String recVol,
    @Part("recWgt") String recWgt,
    @Part("damageQty") String damageQty,
    @Part("looseQty") String looseQty,
    @Part("deliveryDate") String deliveryDate,
    @Part("deliveryTime") String deliveryTime,
    @Part("uploadFile\"; filename=\"audex.jpg\" ") String part, // Change here
    @Part("remarks") String remarks,
    @Part("receivedBy") String receivedBy,
    @Part("ipAddress") String ipAddress
    @Part MultipartBody.Part images
);

@Part("uploadFile\\"; filename=\\"audex.jpg\\" ") String part simply i made changes from RequestBody to string and in last add @Part MultipartBody.Part images that's it. @Part("uploadFile\\"; filename=\\"audex.jpg\\" ") String part我只是从RequestBody更改为字符串,最后添加了@Part MultipartBody.Part images

**Dependency I used is **我使用的依赖是

compile 'com.squareup.retrofit2:retrofit-converters:2.0.2'
compile 'com.squareup.retrofit2:converter-gson:2.0.2'
compile 'com.squareup.retrofit2:retrofit:2.0.2'
compile 'com.squareup.retrofit2:converter-scalars:2.0.2'

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM