Nal*_*llo 8 java post spring android multipartform-data
我已经在我的Android应用程序中成功使用spring for android来从/向服务器获取/发布数据.现在,我必须对多部分表单发布请求,但我一直无法按照我想要的方式运行它.
使用案例:1.从库中选择一张照片2.使用文件源将其加载到位图3.将位图压缩为ByteArrayOutputStream 4.将字节数组(ByteArrayOutputStream.toByteArray())传递给服务器.(我需要发送这个作为jpeg而不是application/octet-stream)
upload photo的服务器端点接受仅具有以下Mime类型的MultipartFile(注意,它不接受MimeType:application/octet-stream):
GIF("image/gif")
PNG("image/png", "image/x-png")
JPG("image/jpeg", "image/jpg", "image/pjpeg")
BMP("image/bmp")
Run Code Online (Sandbox Code Playgroud)
我尝试过使用示例代码,但到目前为止还没有成功.
使用以下代码,我收到以下错误:org.springframework.web.bind.MissingServletRequest ParameterException:必需的MultipartFile参数'file'不存在
非常感谢有关此事的帮助.谢谢,并保持良好的工作.
这是我的代码:
bitmap = BitmapFactory.decodeFile("/mnt/sdcard/DCIM/Camera/20130205_162546.jpg");
ByteArrayOutputStream bos = new ByteArrayOutputStream();
bitmap.compress(CompressFormat.JPEG, 60, bos);
byte[] data = bos.toByteArray();
// populate the data to post
MultiValueMap<String, Object> formData = new LinkedMultiValueMap<String, Object>();
formData.add("caption", "Test Caption");
formData.add("file", data);
// The URL for making the POST request
final String url = "http://api.example.com/imageUpload?oauth_token=XXXXXX";
HttpHeaders requestHeaders = new HttpHeaders();
// Sending multipart/form-data
requestHeaders.setContentType(MediaType.MULTIPART_FORM_DATA);
// Populate the MultiValueMap being serialized and headers in an HttpEntity object to use for the request
HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<MultiValueMap<String, Object>>(formData, requestHeaders);
// Create a new RestTemplate instance
RestTemplate restTemplate = new RestTemplate(true);
// // Set a custom message converter that supports the application/json media type
// final GsonHttpMessageConverter gsonMessageConverter = new GsonHttpMessageConverter();
// gsonMessageConverter.setSupportedMediaTypes(Collections.singletonList(MediaType.APPLICATION_JSON));
// final ByteArrayHttpMessageConverter byteArrayMessageConverter = new ByteArrayHttpMessageConverter();
// byteArrayMessageConverter.setSupportedMediaTypes(Collections.singletonList(MediaType.IMAGE_JPEG));
// final FormHttpMessageConverter formMessageConverter = new FormHttpMessageConverter();
// formMessageConverter.setSupportedMediaTypes(Collections.singletonList(MediaType.IMAGE_JPEG));
// restTemplate.getMessageConverters().addAll(Arrays.asList(gsonMessageConverter, byteArrayMessageConverter, formMessageConverter));
// Make the network request, posting the message, expecting a String in response from the server
ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.POST, requestEntity, String.class);
// Return the response body to display to the user
Log.i(TAG, "**** response.body : " + response.getBody());
Run Code Online (Sandbox Code Playgroud)
gun*_*nar 11
我遇到了同样的问题,解决方案是覆盖org.springframework.core.io.Resource#getFileName()实现.
就我而言:
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);
Run Code Online (Sandbox Code Playgroud)
其中imageFilename是文件名.稍后将包含为multipart标头:Content-Disposition: form-data; name="your_image_form_item"; filename="20130520_142401.jpg"
我希望它有所帮助!
| 归档时间: |
|
| 查看次数: |
13328 次 |
| 最近记录: |