Skip to content Skip to sidebar Skip to footer

Making A Multipart Post Request With Compressed Jpeg Byte Array With Spring For Android

I've been using spring for android successfully in my android app to get/post data from/to the server. Now, I have to do a post request for a multipart form, but I've been unable t

Solution 1:

I've bumped into the same kind of problem and the solution was to override the org.springframework.core.io.Resource#getFileName() implementation.

In my case:

MultiValueMap<String, Object> map = newLinkedMultiValueMap<String, Object>();
// add values to map ... and when it comes to image:Resource res = newByteArrayResource(Utils.uriToByteArray(context, itemImage)) {
                    @OverridepublicStringgetFilename() throws IllegalStateException {
        return imageFileName;
    }
};
HttpHeaders imageHeaders = newHttpHeaders();
imageHeaders.setContentType(MediaType.IMAGE_JPEG);
HttpEntity<Resource> imageEntity = newHttpEntity<Resource>(res, imageHeaders);
map.add("item[image]", imageEntity);

Where imageFilename is the file name. That will be later included as multipart header: Content-Disposition: form-data; name="your_image_form_item"; filename="20130520_142401.jpg"

I hope it helps!

Solution 2:

I too suffered this issue. Turned out that the body of my issue was on the server, server was not configured to handle/resolve multipart request.

Check out my detailed answer here. Hope it helps.

Post a Comment for "Making A Multipart Post Request With Compressed Jpeg Byte Array With Spring For Android"