OpenFeign默认不支持文件上传,需要通过引入Feign的扩展包来实现,添加依赖
    Maven增加依赖:

    1. <dependency>
    2. <groupId>io.github.openfeign.form</groupId>
    3. <artifactId>feign-form</artifactId>
    4. <version>3.8.0</version>
    5. </dependency>
    6. <dependency>
    7. <groupId>io.github.openfeign.form</groupId>
    8. <artifactId>feign-form-spring</artifactId>
    9. <version>3.8.0</version>
    10. </dependency>
    11. <dependency>
    12. <groupId>commons-fileupload</groupId>
    13. <artifactId>commons-fileupload</artifactId>
    14. <version>1.3.3</version>
    15. </dependency>

    添加如下配置类:

    1. @Configuration
    2. public class FeignConfiguration {
    3. @Autowired
    4. private ObjectFactory<HttpMessageConverters> messageConverters;
    5. @Bean
    6. public Encoder feignEncoder() {
    7. return new SpringFormEncoder(new SpringEncoder(messageConverters));
    8. }
    9. }

    调用方FeignService接口:

    1. @FeignClient("client2")
    2. public interface Client2FeignService {
    3. // PostMapping注解的consumes要设置成MediaType.MULTIPART_FORM_DATA_VALUE,不然会直接报错
    4. @PostMapping(value = "/hello/test", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    5. // MultipartFile类型前面用@RequestPart注解修饰,不然文件会传输不过去
    6. String test(@RequestPart(value = "file") MultipartFile file);
    7. }

    文件接收方Controller:

    1. @RestController
    2. @RequestMapping("/hello")
    3. public class HelloController {
    4. @PostMapping("/test")
    5. String test(@RequestPart(value = "file") MultipartFile file) {
    6. System.out.println(file.getSize());
    7. return "ok";
    8. }
    9. }

    参考:https://www.cnblogs.com/wwjj4811/p/15348543.html