使用一部分图像和一部分json创建Android REST多部分请求



我在使用Rest模板、spring-android和注释创建Multipart请求时遇到问题。我发现了许多使用Multipart上传图像或字符串对象的例子,但我找不到任何实现请求的解决方案,即一部分图像和第二部分json。

请求应该类似于

标题:

Content-Type: multipart/form-data; boundary=--abcd12345
Authorization:Basic 1234567890

正文:

--abcd12345
Content-Disposition: form-data; name="photo"; filename="image123.jpg"
Content-Type: image/jpeg
<@INCLUDE *C:UsersJohnDesktopimage123.jpg*@>
--abcd12345
Content-Disposition: form-data; name="item"
Content-Type: application/json
{
    "name": "My item",
    "description": "My item description",
    "categories": [1,2]
}
--abcd12345--

我尝试过许多变体和组合。。。我用MultiValueMap创建了请求,但我所有的努力都在服务器上结束了,服务器给了我无法消费内容类型错误

如果有人知道如何实现这个plz,请告诉我。为了进一步澄清这个问题,我不能使用任何其他库,如apachemime或老式的MultiPartBuilder等。

这是互联网上最常见的例子,其中一部分是图像,另一部分是字符串。

    MultiValueMap<String, Object> parts = new LinkedMultiValueMap<String, Object>();
                parts.add("file", new FileSystemResource(fileToUpload));
                parts.add("method", "hello,world");
                String response = mRestClient.uploadFile(parts);
@Rest(rootUrl = "...", converters = {ByteArrayHttpMessageConverter.class, FormHttpMessageConverter.class, StringHttpMessageConverter.class})
public interface RestClient {
    @Post("")
    public String uploadFile(MultiValueMap data);
}

我的做法:

// convert image to Base64 String
ByteArrayOutputStream stream = new ByteArrayOutputStream();
Bitmap image = getImageFile(imagePath);
image.compress(Bitmap.CompressFormat.JPEG, 100, stream);
byte[] imageBytes = stream.toByteArray();
String encodedImage = Base64.encodeToString(imageBytes, Base64.DEFAULT);
// create new item
ItemCreateDto newItemDto = new ItemCreateDto();
newItemDto.setName(new_item_name.getText().toString());
newItemDto.setDescription(new_item_description.getText().toString());
ObjectMapper obj = new ObjectMapper();
String body = obj.writeValueAsString(newItemDto);
MultiValueMap<String, Object> multiValueMap = new LinkedMultiValueMap<String, Object>();
// create header with content-disposition for image part
HttpHeaders imageHeaders = new HttpHeaders();
imageHeaders.add("Content-Disposition", "form-data; name=photo; filename=photo.jpeg");
imageHeaders.setContentType(MediaType.IMAGE_JPEG);
HttpEntity<String> imageEntity = new HttpEntity<String>(encodedImage, imageHeaders);
// create header for data part
HttpHeaders dataHeaders = new HttpHeaders();
dataHeaders.add("Content-Disposition", "form-data; name=item");
dataHeaders.setContentType(MediaType.APPLICATION_JSON);
HttpEntity<String> dataEntity = new HttpEntity<String>(body, dataHeaders);
// add headers to multiValueMap
multiValueMap.add("photo", imageEntity);
multiValueMap.add("item", dataEntity);
newItemAPI.createItem(multiValueMap);

和AndroidAnnotation+AndroidSpring部分:

@Rest(rootUrl = "", converters = { ByteArrayHttpMessageConverter.class,    MappingJackson2HttpMessageConverter.class, FormHttpMessageConverter.class })
public interface INewItemRestClient {
@Post("")
@Accept(MediaType.APPLICATION_JSON)
@RequiresHeader({"Authorization", "Content-Type"})
String createItem(MultiValueMap<String, Object> data);

希望这能帮助你。。。

相关内容

最新更新