Spring resttemplate - 来自服务器的意外文件结束;嵌套异常是 java.net.SocketException: 来自服务器的意外文件结尾

Spring resttemplate - Unexpected end of file from server; nested exception is java.net.SocketException: Unexpected end of file from server

尝试通过 resttemplate 将照片作为字节数组发送并接收 "Unexpected end of file from server; nested exception is java.net.SocketException: Unexpected end of file from server"。 所有错误 org.springframework.web.client.ResourceAccessException: I/O error on POST request for "http://servername12:8091/rs/photo": Unexpected end of file from server; nested exception is java.net.SocketException: Unexpected end of file from server

但是,如果我发送这个没有结束字节的数组,它发送成功。 我的代码:

    @ResponseBody
    public void uploadFile(@RequestParam("file") MultipartFile file)
    {
        if (!file.isEmpty()) {
            try
            {
                byte[] bytes = file.getBytes();
                HttpHeaders headers = new HttpHeaders();
                headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
                headers.setContentLength(1000000000);
                HttpEntity<byte[]> entity = new HttpEntity<>(bytes, headers);
                RestTemplate restTemplate = new RestTemplate();

                ResponseEntity<ValidatorResponse> response = restTemplate.postForEntity(VALIDATOR_URL + "photo", bytes, ValidatorResponse.class);
                System.out.println(response.getBody().getCode());

            }
            catch (Exception e)
            {
                e.printStackTrace();
            }
        }
    } ```

Google 中只有一个简单的请求,所有第一个链接都显示正确的方法:

使用MediaType.MULTIPART_FORM_DATAResponseEntity<String>