RestTemplate发送MultipartFile为参数的请求

@RequestMapping(value = "/transform2/{path}", method = RequestMethod.POST)
    public Object transfrom2(@PathVariable("path") String path, @RequestParam("multipartFile") MultipartFile multipartFile) throws IOException {
        ServiceNode serviceNode = serviceMap.get(path);
        ByteArrayResource fileAsResource = new ByteArrayResource(multipartFile.getBytes()) {
            @Override
            public String getFilename() {
                return multipartFile.getOriginalFilename();
            }
 
            @Override
            public long contentLength() {
                return multipartFile.getSize();
            }
        };
        MultiValueMap<String, Object> multipartRequest = new LinkedMultiValueMap<>();
        multipartRequest.add("multipartFile", fileAsResource);
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.MULTIPART_FORM_DATA);
        HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity(multipartRequest, headers);
        //发起调用
        return loadBalanced.postForObject(serviceNode.getUrl(), requestEntity, Object.class);
    }

 

posted @ 2020-09-17 23:27  南鸽  阅读(4168)  评论(0编辑  收藏  举报