首页 > 解决方案 > 想要使用改造库在服务器上上传多个文件

问题描述

无法使用改造库在服务器上上传多个文件

在服务器上上传文件时我得到空响应我无法找到解决方案。我得到空响应我的请求是正确的,但仍然有一些错误我如何解决它?

  public interface APIInterfac
    @Multipart
    @POST("/webapi/kycDocumentsUpload")
    Call<KycFileUploadModel> submitData(@Part MultipartBody.Part FileImgPAN,
                                        @Part("FilePan") String FilePan,
                                        @Part("FILEAADHAR") String FILEAADHAR,
                                        @Part("FILEBANKPROOF") String FILEBANKPROOF,
                                        @Part("FILESIGNATURE") String FILESIGNATURE,
                                        @Part("FILEFINACIALPROOF") String FILEFINACIALPROOF,
                                        @Part("FILEPHOTO") String FILEPHOTO,
                                        @Part("FILEIPVPROOF") String FILEIPVPROOF,
                                        @Part("FILEOTHER6") String FILEOTHER6,
                                        @Part("FILEBSEFORMATE") String FILEBSEFORMATE,
                                        @Part("FILENACHFORMATE") String FILENACHFORMATE,
                                        @Part("AutoId") String AutoId,
                                        @Part("LoginId") String LoginId,
                                        @Part("flag") String flag
                                        );

    }

    pojo.java


    public class KycFileUploadModel {

        @SerializedName("T0")
        @Expose
        private List<T0> t0 = null;

        public List<T0> getT0() {
            return t0;
        }

        public void setT0(List<T0> t0) {
            this.t0 = t0;
        }


        public class T0 {

            @SerializedName("result")
            @Expose
            private String result;

            public String getResult() {
                return result;
            }

            public void setResult(String result) {
                this.result = result;
            }
        }
    }

    UploadActivity.java

    public class UploadKycDocumentActivity extends AppCompatActivity {

        private Button btnSubmit, btnImagePan, btnAadhar, btnBankProof, btnSignature, btnFinancialProof, btnImagePhoto, btnipvProof, btnOtherDoc, btnBSEformat, btnNachMendate;
        private TextView tvImagePAN;
        private static final int GALLERY_REQUEST_CODE = 100;
        private static final int CAMERA_CAPTURE_VIDEO_REQUEST_CODE = 200;

        public static final int MEDIA_TYPE_IMAGE = 1;
        public static final int MEDIA_TYPE_VIDEO = 2;

        private static Uri fileUri;
        private TextView tvActionTitle;
        private ImageView imgBack;
        public static Uri selectedImage;
        ArrayList<Uri> list = new ArrayList<>();

        @Override
        protected void onCreate(@Nullable Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_upload_kyc_document);

            //size is not grater than 2 MB

            initView();
            onClick();
            list = new ArrayList<>();

            changeDrawableColor(imgBack, R.color.colorWhite);
            tvActionTitle.setText("Upload Document");

        }

        private void initView() {

            btnSubmit = (Button) findViewById(R.id.btn_submit);
            btnImagePan = (Button) findViewById(R.id.btn_image_pan);
            btnAadhar = (Button) findViewById(R.id.btn_aadhar);
            btnBankProof = (Button) findViewById(R.id.btn_bank_proof);
            btnSignature = (Button) findViewById(R.id.btn_signature);
            btnFinancialProof = (Button) findViewById(R.id.btn_financial_proof);
            btnImagePhoto = (Button) findViewById(R.id.btn_image_photo);
            btnipvProof = (Button) findViewById(R.id.btn_ipv_proof);
            btnOtherDoc = (Button) findViewById(R.id.btn_other_doc);
            btnBSEformat = (Button) findViewById(R.id.btn_bse_format);
            btnNachMendate = (Button) findViewById(R.id.btn_nach_mandate);
            tvImagePAN = (TextView) findViewById(R.id.tv_image_pan);
            tvActionTitle = (TextView) findViewById(R.id.tv_action_title);
            imgBack = (ImageView) findViewById(R.id.img_back);


        }

        @Override
        protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
            super.onActivityResult(requestCode, resultCode, data);

            if (resultCode == Activity.RESULT_OK)
                switch (requestCode) {

                    case 0:
                        //data.getData returns the content URI for the selected Image

                        selectedImage = data.getData();
                        File thePath = new File(getRealPathFromURI(selectedImage));
                        String path = thePath.getPath();

                        list.add(selectedImage);


                        Log.e("FILE PATH IS===", path);


                        //uploadToServer(path);

                        kycUploadDoc("", "", "", "", "", "", "", "", "", "", thePath, "57", "79", "A");
                        tvImagePAN.setText((path));
                        break;

                    case 1:

                        selectedImage = data.getData();
                        File thePath1 = new File(getRealPathFromURI(selectedImage));
                        String path1 = thePath1.getPath();

                        list.add(selectedImage);


                        Log.e("FILE PATH IS===", path1);



                        break;

                }
        }


        private String getRealPathFromURI(Uri contentURI) {

            String thePath = "no-path-found";
            String[] filePathColumn = {MediaStore.Images.Media.DATA};
            Cursor cursor = getContentResolver().query(contentURI, filePathColumn, null, null, null);
            if (cursor.moveToFirst()) {
                int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
                thePath = cursor.getString(columnIndex);
            }
            cursor.close();
            return thePath;
        }


        private void onClick() {

            btnSubmit.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                }
            });

            imgBack.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    onBackPressed();
                }
            });

            btnImagePan.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    pickFromGallery(0);
                    // tvImagePAN.setText(String.valueOf(fileUri));


                }
            });

            btnAadhar.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    pickFromGallery(1);
                    // tvImagePAN.setText(String.valueOf(fileUri));


                }
            });
        }


        private void pickFromGallery(int code) {

            //Create an Intent with action as ACTION_PICK
            Intent intent = new Intent(Intent.ACTION_PICK);
            // Sets the type as image/*. This ensures only components of type image are selected
            intent.setType("image/*");
            //We pass an extra array with the accepted mime types. This will ensure only components with these MIME types as targeted.
            String[] mimeTypes = {"image/jpeg", "image/png"};
            intent.putExtra(Intent.EXTRA_MIME_TYPES, mimeTypes);
            // Launching the Intent
            startActivityForResult(intent, code);
        }


        private void kycUploadDoc(String filePanPath, String fileAadharPath, String fileBankProofPath, String fileSignaturepath, String fileFPPath, String filePhoto, String fileipvProof, String fileOther6, String fileBSEformat, String fileNachPath, File imgPath, String autoId, String loginid, String flag) {


    //        RequestBody requestBody = RequestBody.create(MediaType.parse("multipart/form-data"), imgPath);
    //
    //        MultipartBody.Part image = MultipartBody.Part.createFormData("FileImgPAN", imgPath.getName(), requestBody); //"image" is parameter for photo in API.

            RequestBody requestBody = RequestBody.create(MediaType.parse("*/*"), imgPath);
            MultipartBody.Part fileToUpload = MultipartBody.Part.createFormData("file", imgPath.getName(), requestBody);
            RequestBody filename = RequestBody.create(MediaType.parse("multipart/form-data"), imgPath.getName());
            final APIInterface apiInterface = APIClient.getClient().create(APIInterface.class);
            //KycFileUploadModel uploadDocModel = new KycFileUploadModel(filePanPath, fileAadharPath, fileBankProofPath, fileSignaturepath, fileFPPath, filePhoto, fileipvProof, fileOther6, fileBSEformat, fileNachPath, imgPath, "57", "79", "A");
            Call<KycFileUploadModel> call1 = apiInterface.submitData(fileToUpload, "", "", "", "", "", "", "", "", "", "", "57", "79", "A");
            call1.enqueue(new Callback<KycFileUploadModel>() {
                @Override
                public void onResponse(Call<KycFileUploadModel> uploadDocModel, Response<KycFileUploadModel> response) {

                    Log.e("Response upload SUCCESS==", String.valueOf(response.body()));
                    KycFileUploadModel user1 = response.body();


                }

                @Override
                public void onFailure(Call<KycFileUploadModel> call, Throwable t) {

                    Log.e("Response kyc  upload failure==", t.getMessage());

                }


            });


        }


        private void changeDrawableColor(ImageView img, int color) {

            DrawableCompat.setTint(
                    img.getDrawable(),
                    ContextCompat.getColor(UploadKycDocumentActivity.this, color)
            );
        }

        public static RequestBody createRequestBody(@NonNull File file) {
            return RequestBody.create(MediaType.parse("multipart/form-data"), file);
        }


    }

我的请求是prope>我正在多部分发送文件,并在字符串中发送其他文件的名称与请求。

标签: android

解决方案


您的端点界面看起来像

public interface APIInterfac {
    @Multipart
    @POST("/webapi/kycDocumentsUpload")
    Call<KycFileUploadModel> submitData(@Part List<MultipartBody.Part> FileImgPAN,
                                        @Part("FilePan") String FilePan,
                                        - - -

    }

在您的 Activity/viewModel/Repository 类中,您需要将文件添加到“MultipartBody.Part”类型列表中。所以首先创建列表

List<MultipartBody.Part> parts = new ArrayList<>();

然后创建一个返回“MultipartBody.Part”的方法

private MultipartBody.Part fileToUpload(String partName, String imgPath) {
    File file = new File(imgPath);
    // create RequestBody instance from file
    RequestBody requestFile =
    RequestBody.create(
        MediaType.parse("*/*"),
        file
    );
    // MultipartBody.Part is used to send also the actual file name
    return MultipartBody.Part.createFormData(partName, file.getName(), requestFile);
}

现在您可以通过这种方式将多个文件添加到列表 bu 中。您还可以检查文件是否存在然后通过这种方式添加

parts.add(fileToUpload("filename1", imgPath1));
parts.add(fileToUpload("filename2", imgPath2));

然后通过这种方式拨打电话。

Call<KycFileUploadModel> call1 = apiInterface.submitData(parts, "", "", "", "", "", "", "", "", "", "", "57", "79", "A");
        call1.enqueue(new Callback<KycFileUploadModel>() {
            @Override
            public void onResponse(Call<KycFileUploadModel> uploadDocModel, Response<KycFileUploadModel> response) {
                Log.e("Response upload SUCCESS==", String.valueOf(response.body()));
                KycFileUploadModel user1 = response.body();
            }

            @Override
            public void onFailure(Call<KycFileUploadModel> call, Throwable t) {
                Log.e("Response kyc  upload failure==", t.getMessage());
            }
        });

推荐阅读