无法通过 RestTemplate 将文件上传到 Spring REST 服务

Unable to upload a file to Spring REST service via RestTemplate

我写了一个小的Spring控制器,它接受一个多部分文件作为参数

@PutMapping("/fileUpload")
    public String test(@RequestParam("test") MultipartFile file) {
        System.out.println("In controller");
        return file.getOriginalFilename();
    }

我使用的对应的RestTemplate是

Path path = Paths.get("C:\Users\Foo\Desktop", "baz.txt");

            FormHttpMessageConverter messageConverter = new FormHttpMessageConverter();
            messageConverter.addPartConverter(new ByteArrayHttpMessageConverter());

            RestTemplate client = new RestTemplate();
            client.getMessageConverters().add(messageConverter);

            String end_url = "http://localhost:8888/test/fileUpload";

            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.MULTIPART_FORM_DATA);

            MultiValueMap<String, Object> body = new LinkedMultiValueMap<String, Object>();

            body.add("test", new ByteArrayResource(Files.readAllBytes(path)));

            HttpEntity<MultiValueMap<String, Object>> entity 
                = new HttpEntity<MultiValueMap<String, Object>>(body, headers);

            client.exchange(end_url, HttpMethod.PUT, entity, String.class, new HashMap()); 

无论我尝试什么,都不断收到以下错误:

Caused by: org.springframework.web.client.HttpClientErrorException: 400 null
    at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:91) ~[spring-web-4.3.6.RELEASE.jar:4.3.6.RELEASE]
    at org.springframework.web.client.RestTemplate.handleResponse(RestTemplate.java:700) ~[spring-web-4.3.6.RELEASE.jar:4.3.6.RELEASE]
    at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:653) ~[spring-web-4.3.6.RELEASE.jar:4.3.6.RELEASE]
    at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:621) ~[spring-web-4.3.6.RELEASE.jar:4.3.6.RELEASE]
    at org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:540) ~[spring-web-4.3.6.RELEASE.jar:4.3.6.RELEASE]
    at org.controller.Runner.run(Runner.java:57) [classes/:na]
    at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:776) [spring-boot-1.5.1.RELEASE.jar:1.5.1.RELEASE]
    ... 6 common frames omitted

我做错了什么?我尝试添加所有典型的消息转换器,如 ByteArrayHttpMessageConverter、ResourceHttpMessageConverter,我还尝试使用 commons-fileupload

谁能告诉我我的 RestTemplate 做错了什么?

你好吗? 尝试修改您的

@PutMapping("/fileUpload") 

@PutMapping(value = "/fileUpload", consumes = "multipart/form-data")