0

I want to send Multipart file from android device to rest service which has developed in Spring.

Here is my android code..

 @Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == 1 && resultCode == RESULT_OK && data != null && data.getData() != null) {

      Uri filePath = data.getData();

        try {
            bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), filePath);
            imageView.setImageBitmap(bitmap);

        } catch (IOException e) {
            e.printStackTrace();
        }

        try {

            String path = getPath(filePath);
            //Creating a multi part request
            new MultipartUploadRequest(this,"http://localhost:8080/samplerestfullservice/classtest/upload1")
                    .addFileToUpload(path, "image")//Adding file 
                    .setNotificationConfig(new UploadNotificationConfig())
                    .setMaxRetries(2)
                    .startUpload(); //Starting the upload

        } catch (Exception exc) {
            Toast.makeText(this, exc.getMessage(), Toast.LENGTH_SHORT).show();
        }
    }
}


 public String getPath(Uri uri) {
    String[] projection={MediaStore.Images.Media.DATA};
    Cursor cursor=getContentResolver().query(uri,projection,null,null,null);
    if(cursor == null){
        return null;
    }
    int columnIndex= cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
    cursor.moveToFirst();
    String s=cursor.getString(columnIndex);
    cursor.close();
    return s;
}

And my service code is

@RequestMapping(value = "/upload1", method = RequestMethod.POST, headers = "Content-Type= multipart/form-data" )
@ResponseBody
public void fileUpload(@RequestParam MultipartFile file){       

    System.out.print("Success");

    // rest of the code

}

And I have declared internet permission in manifest.xml but the client request doesn't reach the service.And I don't know is there any compatibility problem.

Please can any one give suggestion.

Thank You.

4
  • shouldn't this .addFileToUpload(path, "image") be like .addFileToUpload("file_name", "path")? Commented Nov 10, 2018 at 5:32
  • Sorry Ali I didn't get you.."file-name" in the sense file path right Commented Nov 10, 2018 at 5:41
  • you have write the path as 1st argument. i think so it should be 2nd. Commented Nov 10, 2018 at 5:45
  • No Ali 1st argument should be path and 2nd should be parameter name like this......MultipartUploadRequest addFileToUpload(final String path, final String parameterName) Commented Nov 10, 2018 at 5:50

1 Answer 1

1

I have wrote lightweight and easy to use library that solves your problem. It is described here https://stackoverflow.com/a/53253933/8849079:)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.