使用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 = "http://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());
    }

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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容