RestTemplate发送文件

young 536 2021-10-17
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.MULTIPART_FORM_DATA);
MultiValueMap<String, Object> paramMap = new LinkedMultiValueMap<>();
paramMap.add("file", new FileSystemResource(file));
HttpEntity<MultiValueMap<String, Object>> httpEntity = new HttpEntity<>(paramMap, headers);
restTemplate.postForObject(url,httpEntity, String.class);