i have some problem about send image file (multipart) using android via Spring restTemplate.
here is my controller on server :
@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);
}
and this is rest request in my android activity
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();
}
I get this error :
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)
Whats wrong with my code ? Any HttpMessageConverter for Multipart Request ?
thanks
FormHttpMessageConverter
cannot write the expenseId
part because it is an integer. Try sending it as a String:
map.add("expenseId", "1");
And receiving as a String:
@PathVariable("id") String id
There is a very similar question and answer here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With