我有一些困难(我上面只花了一个下午)用InputStreamResource
上传图片,这是我目前的代码:
MultiValueMap<String, Object> parts = new LinkedMultiValueMap<String, Object>();
ByteArrayOutputStream bos = new ByteArrayOutputStream();
bitmap.compress(CompressFormat.PNG, 0, bos);
bos.toByteArray();
ByteArrayInputStream bs = new ByteArrayInputStream(bos.toByteArray());
parts.add("photo", new InputStreamResource(bs));
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
HttpEntity<MultiValueMap<String, Object>> request = new HttpEntity<MultiValueMap<String, Object>>(parts, headers);
RestTemplate template = getRestTemplate();
template.setErrorHandler(new MyDefaultReponseErrorHandler());
template.getMessageConverters().add(new FormHttpMessageConverter());
return template.postForObject("http://bottlapp.cleverapps.io/api/pic", request, Result.class);
和生成的痕迹:
03-29 19:00:45.438:E // RequestRunner.java:134(18769):19:00:45.454 Thread-28593请求网络执行期间发生异常:org.springframework.core.io.InputStreamResource不能被强制转换为java.lang.String 03-29 19:00:45.438:E // RequestRunner.java:134(18769):java.lang.ClassCastException:org.springframework.core.io.InputStreamResource无法强制转换为java.lang.String
有人可以帮助我吗?
谢谢你。