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

 

上一篇:Controller同时接收MultipartFile数据和json数据


下一篇:Java-File转MultipartFile