Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java POST InputStream RestTemplate_Java_Spring_Post_Inputstream_Resttemplate - Fatal编程技术网

Java POST InputStream RestTemplate

Java POST InputStream RestTemplate,java,spring,post,inputstream,resttemplate,Java,Spring,Post,Inputstream,Resttemplate,我想将InputStream发布到服务器。 我使用Spring和RestTemplate来执行HTTP请求 客户 public void postSomething(InputStream inputStream) { String url = "localhost:8080/example/id RestTemplate restTemplate = new RestTemplate(); restTemplate.

我想将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;
}
在客户端,我得到
没有[java.io.ByteArrayInputStream]的HttpMessageConverter
在服务器端,我得到
无法构造“java.io.InputStream”的实例(不存在像默认构造函数那样的创建者):抽象类型要么需要映射到具体类型,具有自定义反序列化器,要么包含额外的类型信息

用于
字节[]
,而不是
InputStream
,就像这个班的名字说的

没有内置的
InputStream
,但是有一个,可以处理,例如

restemplate restemplate=new restemplate(Arrays.asList(new resourcehttmpessageconverter());
URI location=restTemplate.postForLocation(url,新的InputStreamResource(inputStream));

您不能通过HTTP传输流。HTTP是一种无状态协议,不支持字节传输。你能做的就是

  • 读取整个输入流并将其作为字符串或任何其他形式输出
  • 使用任何UDP形式的数据传输(SOAP或套接字)

  • 为什么不改用multipart呢?非常感谢。它解决了这个问题。你让我开心:)