POST InputStream RestTemplate

huangapple go评论81阅读模式
英文:

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,就像类名所示。

没有内置的InputStreamHttpMessageConverter,但有一个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

  1. Read the whole input stream and output it as a string or any other form
  2. Use any UDP form of data transfer (SOAP or sockets)

huangapple
  • 本文由 发表于 2020年9月8日 21:27:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/63794946.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定