閱讀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被鎖定怎麼辦