通过RestTemplate发送多部分文件
我有一些关于使用android通过Spring restTemplate发送图像文件(multipart)的问题。
这里是我的服务器控制器:
@RequestMapping(value = "/uploadPhoto/{id}", method = RequestMethod.POST)
@ResponseBody
public ResponseEntity<String> uploadPhoto(@RequestParam("file") MultipartFile srcFile,
@PathVariable("id") Integer id) {
/// do something
return RestUtil.getJsonSHttptatus(HttpStatus.NOT_ACCEPTABLE);
}
这是我的android活动中的休息请求
private void doUpload(){
new AsyncTask<String, Void, String>() {
@Override
protected String doInBackground(String... params) {
FormHttpMessageConverter formHttpMessageConverter = new FormHttpMessageConverter();
formHttpMessageConverter.setCharset(Charset.forName("UTF8"));
RestTemplate restTemplate = new RestTemplate();
restTemplate.getMessageConverters().add( formHttpMessageConverter );
restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
restTemplate.setRequestFactory(new HttpComponentsClientHttpRequestFactory());
String uri = "http://localhost:8089/web/uploadPhoto/1";
String imagePath = "/mnt/sdcard/DCIM/Camera/IMG_20140406_130350.jpg";
MultiValueMap<String, Object> map = new LinkedMultiValueMap<String, Object>();
map.add("expenseId", 1);
map.add("file", new FileSystemResource(path));
HttpHeaders imageHeaders = new HttpHeaders();
imageHeaders.setContentType(MediaType.MULTIPART_FORM_DATA);
HttpEntity<MultiValueMap<String, Object>> imageEntity = new HttpEntity<MultiValueMap<String, Object>>(map, imageHeaders);
restTemplate.exchange(uri, HttpMethod.POST, imageEntity, Boolean.class);
return "";
}
@Override
protected void onPostExecute(String result) {
Toast.makeText(MainActivity.this, "Data : "+result,
Toast.LENGTH_LONG).show();
}
}.execute();
}
我得到这个错误:
04-07 07:21:00.312: E/AndroidRuntime(1205): Caused by: org.springframework.http.converter.HttpMessageNotWritableException: Could not write request: no suitable HttpMessageConverter found for request type [java.lang.Integer]
04-07 07:21:00.312: E/AndroidRuntime(1205): at org.springframework.http.converter.FormHttpMessageConverter.writePart(FormHttpMessageConverter.java:314)
04-07 07:21:00.312: E/AndroidRuntime(1205): at org.springframework.http.converter.FormHttpMessageConverter.writeParts(FormHttpMessageConverter.java:276)
04-07 07:21:00.312: E/AndroidRuntime(1205): at org.springframework.http.converter.FormHttpMessageConverter.writeMultipart(FormHttpMessageConverter.java:266)
04-07 07:21:00.312: E/AndroidRuntime(1205): at org.springframework.http.converter.FormHttpMessageConverter.write(FormHttpMessageConverter.java:209)
04-07 07:21:00.312: E/AndroidRuntime(1205): at org.springframework.http.converter.FormHttpMessageConverter.write(FormHttpMessageConverter.java:91)
04-07 07:21:00.312: E/AndroidRuntime(1205): at org.springframework.web.client.RestTemplate$HttpEntityRequestCallback.doWithRequest(RestTemplate.java:624)
04-07 07:21:00.312: E/AndroidRuntime(1205): at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:474)
04-07 07:21:00.312: E/AndroidRuntime(1205): at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:439)
04-07 07:21:00.312: E/AndroidRuntime(1205): at org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:415)
04-07 07:21:00.312: E/AndroidRuntime(1205): at com.sample.rest.client.MainActivity$4.doInBackground(MainActivity.java:295)
04-07 07:21:00.312: E/AndroidRuntime(1205): at com.sample.rest.client.MainActivity$4.doInBackground(MainActivity.java:1)
我的代码有什么问题? 任何用于多部分请求的HttpMessageConverter?
谢谢
FormHttpMessageConverter
无法编写expenseId
部分,因为它是一个整数。 尝试将它作为字符串发送:
map.add("expenseId", "1");
并以字符串形式接收:
@PathVariable("id") String id
这里有一个非常相似的问题和答案。
链接地址: http://www.djcxy.com/p/78593.html上一篇: Send multipart file via RestTemplate
下一篇: AngularJS set element fixed position based on another element