当前位置:   article > 正文

用RestTemplate发送MultipartFile为参数的请求

resttemplate发送multipartfile
```
@Autowired
RestTemplate restTemplate;
@PostMapping("/send")
public TopicFileResponse send(String url,String path) {

    HttpHeaders headers = new HttpHeaders();
    MediaType type = MediaType.parseMediaType("multipart/form-data");
    // 设置请求的格式类型
    headers.setContentType(type);
    FileSystemResource fileSystemResource = new FileSystemResource(path);
    MultiValueMap<String, Object> form = new LinkedMultiValueMap<>();
    form.add("name", fileSystemResource);
    HttpEntity<MultiValueMap<String, Object>> files = new HttpEntity<>(form, headers);
    ResponseEntity<TopicFileResponse> responseResponseEntity = restTemplate.postForEntity(url, files, TopicFileResponse.class);
    TopicFileResponse body = responseResponseEntity.getBody();
    return body;
}
```
> 其中TopicFileResponse是对方请求该返回的结果
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/AllinToyou/article/detail/302892
推荐阅读
相关标签
  

闽ICP备14008679号