public static void main(String[] args) {
final String filePath = "d:";
final String fileName = "123.pdf";
final String url = "http://localhost:8080/file/upload";
RestTemplate restTemplate = new RestTemplate();
//设置请求头
HttpHeaders headers = new HttpHeaders();
MediaType type = MediaType.parseMediaType("multipart/form-data");
headers.setContentType(type);
//设置请求体,注意是LinkedMultiValueMap
FileSystemResource fileSystemResource = new FileSystemResource(filePath+"/"+fileName);
MultiValueMap<String, Object> form = new LinkedMultiValueMap<>();
form.add("file", fileSystemResource);
form.add("filename",fileName);
//用HttpEntity封装整个请求报文
HttpEntity<MultiValueMap<String, Object>> files = new HttpEntity<>(form, headers);
Map map = restTemplate.postForObject(url, files, Map.class);
}
restTmplate发送文件类型请求
最新推荐文章于 2023-09-22 10:36:37 发布