Making a multipart post request with compressed jpeg byte array with spring for android

烂漫一生 提交于 2019-12-03 14:07:35

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 = new LinkedMultiValueMap<String, Object>();
// add values to map ... and when it comes to image:
Resource res = new ByteArrayResource(Utils.uriToByteArray(context, itemImage)) {
                    @Override
    public String getFilename() throws IllegalStateException {
        return imageFileName;
    }
};
HttpHeaders imageHeaders = new HttpHeaders();
imageHeaders.setContentType(MediaType.IMAGE_JPEG);
HttpEntity<Resource> imageEntity = new HttpEntity<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!

TrueCoke

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!