我在使用Rest模板,spring–android和注释创建Multipart请求时遇到问题.我发现了许多使用Multipart上载图像或String对象的示例,但我找不到实现一部分图像和第二部分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创建请求,但是我所有的努力都以服务器告终,这给我带来了无法消耗内容类型错误的麻烦
如果有人知道如何执行此plz,请告诉.为了进一步澄清问题,我不能使用其他任何库,例如apache mime或旧式的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和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);
希望这个能对您有所帮助…