阅读682 返回首页    go 阿里云 go 技术社区[云栖]


使用RestTemplate上传文件

最近在用Spring Cloud,搭建微服务应用,其中一个微服务是把文件上传到七牛,其他的文件上传都是通过他。但是在使用Fegin调用该服务的接口的时候,一直有问题,恩--------先用RestTemplate试试


博客小福利 点我 阿里云优惠券免费取


步骤

1、声明对象

    @Bean
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }

2、发送请求

    @Autowired
    private RestTemplate rest;
    public void ExceptInfoForRestTemplate(String excelTitle, List<String[]> arrayList) throws
            ParseException {

        String fileLocal="E://xxccccccccc"
        String url = "https://xxxxxx.com/upload";
        FileSystemResource resource = new FileSystemResource(new File(fileLocal));
        MultiValueMap<String, Object> param = new LinkedMultiValueMap<>();
        param.add("file", resource);

        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());
    }

我的官网https://guan2ye.com
我的CSDN地址https://blog.csdn.net/chenjianandiyi
我的简书地址https://www.jianshu.com/u/9b5d1921ce34
我的githubhttps://github.com/javanan
我的码云地址https://gitee.com/jamen/

最后更新:2017-11-03 13:03:35

  上一篇:go  docker清理日志
  下一篇:go  新买的6splus被锁定怎么办