我想向服务器发布一个InputStream。我使用Spring,因此使用RestTemplate来执行我的HTTP请求。
客户端
public void postSomething(InputStream inputStream) {
String url = "localhost:8080/example/id";
RestTemplate restTemplate = new RestTemplate();
restTemplate.getMessageConverters().add(new ByteArrayHttpMessageConverter());
restTemplate.postForLocation(url, inputStream, InputStream.class);
}
服务器
@PostMapping("/example/{id}")
public void uploadFile(@RequestBody InputStream inputStream, @PathVariable String id) {
InputStream inputStream1 = inputStream;
}
在客户端,我得到No HttpMessageConverter for [java.io.ByteArrayInputStream]
,在服务器端,我得到Cannot construct instance of 'java.io.InputStream' (no Creators, like default constructor, exist): abstract types either need to be mapped to concrete types, have custom deserializer, or contain additional type information
发布于 2020-09-08 13:45:03
ByteArrayHttpMessageConverter
代表的是byte[]
,而不是InputStream
,就像类名所说的那样。
没有内置的HttpMessageConverter
用于InputStream
,但是有一个ResourceHttpMessageConverter
,它可以处理例如InputStreamResource
。
RestTemplate restTemplate = new RestTemplate(Arrays.asList(new ResourceHttpMessageConverter()));
URI location = restTemplate.postForLocation(url, new InputStreamResource(inputStream));
发布于 2020-09-08 13:45:53
不能通过HTTP传输流。HTTP是一种无状态协议,它不支持字节传输。你能做的就是
。
https://stackoverflow.com/questions/63794946
复制相似问题