Q:

How to send a large input stream to a Spring REST service?

Have a Spring Rest application that run inside an embedded Jetty container.
On Client I use RestTemplate(try to).
Use case :
Having an InputStream (I don't have the File), I want to send it to the REST service.
The InputStream can be quite large (no byte[] !).
What I've tried so far :
Added StandardServletMultipartResolver to the Dispatcher context;
On servlet registration executed :
ServletRegistration.Dynamic dispatcher = ...
MultipartConfigElement multipartConfigElement = new MultipartConfigElement("D:/temp");
dispatcher.setMultipartConfig(multipartConfigElement);

On client :
restTemplate.getMessageConverters().add(new FormHttpMessageConverter());
        MultiValueMap<String, Object> parts = new LinkedMultiValueMap<String, Object>();
        parts.add("attachmentData", new InputStreamResource(data) {
            // hacks ...
            @Override
            public String getFilename() {
                //avoid null file name
                return "attachment.zip";
            }

            @Override
            public long contentLength() throws IOException {
                // avoid calling getInputStream() twice
                return -1L;
            }
        }
ResponseEntity<Att> saved = restTemplate.postForEntity(url, parts, Att.class)

On server :
    @RequestMapping("/attachment")
    public ResponseEntity<Att> saveAttachment(@RequestParam("attachmentData") javax.servlet.http.Part part) {
        try {
            InputStream is = part.getInputStream();
            // consume is
            is.close();
            part.delete();
            return new ResponseEntity<Att>(att, HttpStatus.CREATED);
        } 
    }

What is happening :
The uploaded InputStream is stored successfully in the configured temp folder (MultiPart1970755229517315824), the Part part parameter is correctly Injected in the handler method.
The delete() method does not delete the file (smth still has opened handles on it).
Anyway it looks very ugly.
Is there a smoother solution ?

A:

You want to use HTTP's Chunked Transfer Coding. You can enable that by setting SimpleClientHttpRequestFactory.setBufferRequestBody(false). See SPR-7909.

