how to send multi part file from android to rest web service(Spring)?











up vote
0
down vote

favorite












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.










share|improve this question






















  • shouldn't this .addFileToUpload(path, "image") be like .addFileToUpload("file_name", "path")?
    – Ali Ahmed
    Nov 10 at 5:32










  • Sorry Ali I didn't get you.."file-name" in the sense file path right
    – Sunil T
    Nov 10 at 5:41












  • you have write the path as 1st argument. i think so it should be 2nd.
    – Ali Ahmed
    Nov 10 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)
    – Sunil T
    Nov 10 at 5:50

















up vote
0
down vote

favorite












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.










share|improve this question






















  • shouldn't this .addFileToUpload(path, "image") be like .addFileToUpload("file_name", "path")?
    – Ali Ahmed
    Nov 10 at 5:32










  • Sorry Ali I didn't get you.."file-name" in the sense file path right
    – Sunil T
    Nov 10 at 5:41












  • you have write the path as 1st argument. i think so it should be 2nd.
    – Ali Ahmed
    Nov 10 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)
    – Sunil T
    Nov 10 at 5:50















up vote
0
down vote

favorite









up vote
0
down vote

favorite











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.










share|improve this question













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.







java android multipart






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 10 at 5:03









Sunil T

64




64












  • shouldn't this .addFileToUpload(path, "image") be like .addFileToUpload("file_name", "path")?
    – Ali Ahmed
    Nov 10 at 5:32










  • Sorry Ali I didn't get you.."file-name" in the sense file path right
    – Sunil T
    Nov 10 at 5:41












  • you have write the path as 1st argument. i think so it should be 2nd.
    – Ali Ahmed
    Nov 10 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)
    – Sunil T
    Nov 10 at 5:50




















  • shouldn't this .addFileToUpload(path, "image") be like .addFileToUpload("file_name", "path")?
    – Ali Ahmed
    Nov 10 at 5:32










  • Sorry Ali I didn't get you.."file-name" in the sense file path right
    – Sunil T
    Nov 10 at 5:41












  • you have write the path as 1st argument. i think so it should be 2nd.
    – Ali Ahmed
    Nov 10 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)
    – Sunil T
    Nov 10 at 5:50


















shouldn't this .addFileToUpload(path, "image") be like .addFileToUpload("file_name", "path")?
– Ali Ahmed
Nov 10 at 5:32




shouldn't this .addFileToUpload(path, "image") be like .addFileToUpload("file_name", "path")?
– Ali Ahmed
Nov 10 at 5:32












Sorry Ali I didn't get you.."file-name" in the sense file path right
– Sunil T
Nov 10 at 5:41






Sorry Ali I didn't get you.."file-name" in the sense file path right
– Sunil T
Nov 10 at 5:41














you have write the path as 1st argument. i think so it should be 2nd.
– Ali Ahmed
Nov 10 at 5:45




you have write the path as 1st argument. i think so it should be 2nd.
– Ali Ahmed
Nov 10 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)
– Sunil T
Nov 10 at 5:50






No Ali 1st argument should be path and 2nd should be parameter name like this......MultipartUploadRequest addFileToUpload(final String path, final String parameterName)
– Sunil T
Nov 10 at 5:50














1 Answer
1






active

oldest

votes

















up vote
0
down vote













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






share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53236131%2fhow-to-send-multi-part-file-from-android-to-rest-web-servicespring%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote













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






    share|improve this answer

























      up vote
      0
      down vote













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






      share|improve this answer























        up vote
        0
        down vote










        up vote
        0
        down vote









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






        share|improve this answer












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







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 18 at 14:36









        Igor

        1044




        1044






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53236131%2fhow-to-send-multi-part-file-from-android-to-rest-web-servicespring%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Schultheiß

            Liste der Kulturdenkmale in Wilsdruff

            Android Play Services Check