当前位置:   article > 正文

SpringBoot RestTemplate 上传文件

SpringBoot RestTemplate 上传文件

SpringBoot RestTemplate 上传文件

  1. @Test
  2. public void testUpload() throws Exception {
  3. String url = "http://127.0.0.1/file/upload";
  4. String filePath = "C:\\temp\\1.png";
  5. RestTemplate rest = new RestTemplate();
  6. FileSystemResource resource = new FileSystemResource(new File(filePath));
  7. MultiValueMap<String, Object> param = new LinkedMultiValueMap<>();
  8. param.add("file", resource);
  9. param.add("fid", "1");
  10. String string = rest.postForObject(url, param, String.class);
  11. System.out.println(string);
  12. }

或者

  1. @Test
  2. public void testUpload2() throws Exception {
  3. String url = "http://127.0.0.1/file/upload";
  4. String filePath = "C:\\temp\\1.png";
  5. RestTemplate rest = new RestTemplate();
  6. FileSystemResource resource = new FileSystemResource(new File(filePath));
  7. MultiValueMap<String, Object> param = new LinkedMultiValueMap<>();
  8. param.add("file", resource);
  9. param.add("fid", "1");
  10. HttpEntity<MultiValueMap<String, Object>> httpEntity = new HttpEntity<MultiValueMap<String, Object>>(param);
  11. ResponseEntity<String> responseEntity = rest.exchange(url, HttpMethod.POST, httpEntity, String.class);
  12. System.out.println(responseEntity.getBody());
  13. }

SpringBoot项目 前后端分离 ajax附件上传下载,参考SpringBoot项目 前后端分离 ajax附件上传下载

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/程序诗人/article/detail/60811
推荐阅读
相关标签
  

闽ICP备14008679号