SpringBoot RestTemplate 上传文件

SpringBoot RestTemplate 上传文件

    @Test
    public void testUpload() throws Exception {
        String url = "http://127.0.0.1/file/upload";
        String filePath = "C:\\temp\\1.png";

        RestTemplate rest = new RestTemplate();
        FileSystemResource resource = new FileSystemResource(new File(filePath));
        MultiValueMap<String, Object> param = new LinkedMultiValueMap<>();
        param.add("file", resource);
        param.add("fid", "1");

        String string = rest.postForObject(url, param, String.class);

        System.out.println(string);
    }

或者

    @Test
    public void testUpload2() throws Exception {

        String url = "http://127.0.0.1/file/upload";
        String filePath = "C:\\temp\\1.png";

        RestTemplate rest = new RestTemplate();
        FileSystemResource resource = new FileSystemResource(new File(filePath));
        MultiValueMap<String, Object> param = new LinkedMultiValueMap<>();
        param.add("file", resource);
        param.add("fid", "1");

        HttpEntity<MultiValueMap<String, Object>> httpEntity = new HttpEntity<MultiValueMap<String, Object>>(param);
        ResponseEntity<String> responseEntity = rest.exchange(url, HttpMethod.POST, httpEntity, String.class);

        System.out.println(responseEntity.getBody());

    }

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

相关推荐

  1. nestjs文件

    2024-02-04 07:06:04       66 阅读
  2. springMVC-文件

    2024-02-04 07:06:04       68 阅读
  3. 文件

    2024-02-04 07:06:04       58 阅读
  4. springboot 文件

    2024-02-04 07:06:04       46 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-02-04 07:06:04       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-02-04 07:06:04       100 阅读
  3. 在Django里面运行非项目文件

    2024-02-04 07:06:04       82 阅读
  4. Python语言-面向对象

    2024-02-04 07:06:04       91 阅读

热门阅读

  1. clickhouse query log

    2024-02-04 07:06:04       42 阅读
  2. Web后端:CSRF攻击及应对方法

    2024-02-04 07:06:04       48 阅读
  3. Vue组件通信讲解[父子组件通信]

    2024-02-04 07:06:04       41 阅读
  4. 【Django-ninja】使用Django ninja 进行auth鉴权

    2024-02-04 07:06:04       42 阅读