通过RestTemplate发送multipart文件

时间:2014-04-07 07:55:57

标签: android spring rest resttemplate spring-android

我有一些关于使用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?

谢谢

1 个答案:

答案 0 :(得分:4)

FormHttpMessageConverter无法写入expenseId部分,因为它是一个整数。尝试将其作为字符串发送:

map.add("expenseId", "1");

以字符串形式接收:

@PathVariable("id") String id

有一个非常相似的问题和答案here