英文:
POST InputStream RestTemplate
问题
Client
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);
}
Server
@PostMapping("/example/{id}")
public void uploadFile(@RequestBody InputStream inputStream, @PathVariable String id) {
InputStream inputStream1 = inputStream;
}
在客户端,我想将一个 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
的错误消息。
英文:
I want to POST an InputStream to the Server.
I'm using Spring and therefore RestTemplate to execute my HTTP Requests.
Client
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);
}
Server
@PostMapping("/example/{id}")
public void uploadFile(@RequestBody InputStream inputStream, @PathVariable String id) {
InputStream inputStream1 = inputStream;
}
On Client side I get
No HttpMessageConverter for [java.io.ByteArrayInputStream]
And on Server side I get 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
答案1
得分: 1
ByteArrayHttpMessageConverter
适用于byte[]
,而不是InputStream
,就像类名所示。
没有内置的InputStream
的HttpMessageConverter
,但有一个ResourceHttpMessageConverter
,可以处理例如InputStreamResource
。
RestTemplate restTemplate = new RestTemplate(Arrays.asList(new ResourceHttpMessageConverter()));
URI location = restTemplate.postForLocation(url, new InputStreamResource(inputStream));
英文:
ByteArrayHttpMessageConverter
is for byte[]
, not InputStream
, just like the name of the class says.
There are no built-in HttpMessageConverter
for InputStream
, but there is a ResourceHttpMessageConverter
, which can handle e.g. an InputStreamResource
.
RestTemplate restTemplate = new RestTemplate(Arrays.asList(new ResourceHttpMessageConverter()));
URI location = restTemplate.postForLocation(url, new InputStreamResource(inputStream));
答案2
得分: -3
你不能通过HTTP传输流。HTTP是一种无状态协议,不支持字节传输。你可以做以下操作:
1)将整个输入流读取并输出为字符串或其他形式。
2)使用任何UDP形式的数据传输(如SOAP或套接字)。
英文:
You cannot transfer streams over HTTP. HTTP is a stateless protocol and it won't support byte transfer. What you can do is
- Read the whole input stream and output it as a string or any other form
- Use any UDP form of data transfer (SOAP or sockets)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论