服务A

 /**
     * 文件上传
     *
     * @param file
     */
    @PostMapping("upload")
    public Map<String,Object> upload(@RequestParam("file") MultipartFile file) {

        String uploadUrl = fileServiceImpl.upload(file);
        HashMap<String, Object> map = new HashMap<>();
        map.put("url",uploadUrl);
        map.put("message","文件上传成功");
        return map;
    }

服务B

 @Autowired
    RestTemplate template;
    

    @PostMapping(value = "/oss/upload")
    public Map<String,Object>  upload(@RequestParam("file") MultipartFile file) throws IOException {
        MultiValueMap<String, Object> bodyParams = new LinkedMultiValueMap<>();
        org.springframework.core.io.Resource resource = new ByteArrayResource(file.getBytes()){
            @Override
            public String getFilename() {
                return "img";
            }
        };
        bodyParams.add("file", resource);
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.MULTIPART_FORM_DATA);
        HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(bodyParams, headers);
        Map<String,Object> map = template.postForObject("http://service-oss/oss/file/upload", requestEntity, Map.class);
        return map;
    }

调用服务B的接口上传文件

demo.jpg

最后修改:2020 年 09 月 27 日 11 : 38 PM
如果觉得我的文章对你有用,请随意赞赏