【问题标题】:Create Android REST multipart request with one part image and one part json使用一份图像和一份 json 创建 Android REST 多部分请求
【发布时间】:2015-01-06 15:29:19
【问题描述】:

我在使用 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:\Users\John\Desktop\image123.jpg*@>
--abcd12345
Content-Disposition: form-data; name="item"
Content-Type: application/json

{
    "name": "My item",
    "description": "My item description",
    "categories": [1,2]
}
--abcd12345--

我尝试了许多变化和组合...我使用 MultiValueMap 创建请求,但我所有的努力都以服务器结束,给我无法使用内容类型错误

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

这是 Internet 上最常见的示例,它具有一部分图像和另一部分字符串。

    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);
}

【问题讨论】:

    标签: android json rest multipart spring-android


    【解决方案1】:

    我的做法:

    // 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 + Android Spring部分:

    @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);
    

    希望这对你有帮助...

    【讨论】:

    • 非常感谢!这正是我所需要的... BRAVO Good Sir!
    猜你喜欢
    • 2021-12-11
    • 1970-01-01
    • 2014-10-04
    • 2016-11-07
    • 2014-02-10
    • 2011-11-27
    • 2020-11-14
    • 2017-09-12
    • 2019-12-09
    相关资源
    最近更新 更多