1,API 工程

       定义上传文件接口

       

    @FeignClient(value = "PLACESERVICEPROVIDER", configuration = FileService.MutilSupportConfig.class)
    public interface FileService {
    
        @PostMapping(value = "/file/upload", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
        public BaseResult upload(@RequestPart(value = "file") MultipartFile file) throws ServiceException;
    
    @Configuration
    class MutilSupportConfig {
    
            @Autowired
    ObjectFactory<HttpMessageConverters> messageConvertos;
    
    @Bean
    public Encoder feignFormEncoder() {
                return new SpringFormEncoder(new SpringEncoder(messageConvertos));
    }
        }
    }




     

    2,服务方工程

      具体实现如下

      

    @Service
    @RestController
    public class FileServiceImpl implements FileService {
        @Override
        public BaseResult upload(MultipartFile file) throws ServiceException {
            log.info("====<<<<进入上传处理接口");
            if (file == null) {
                return BaseResultGenerator.failure("上传文件file不能为空");
            }
            return BaseResultGenerator.success(file.getOriginalFilename());
        }
    }

      

     

    3,消费方工程

      具体实现如下

     

    @Slf4j
    @RestController
    public class FileController {
    
        @Autowired
        FileService fileService;
    
        @PostMapping(value = "/file/upload", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
        public BaseResult upload(MultipartFile file) throws ServiceException {
            log.debug("=============>进入上传接口{}", file.getName());
            return fileService.upload(file);
        }
    }



     

    4. 要引入的包

     <dependency>
                    <groupId>io.github.openfeign.form</groupId>
                    <artifactId>feign-form-spring</artifactId>
                    <version>3.2.2</version>
                </dependency>
                <dependency>
                    <groupId>io.github.openfeign.form</groupId>
                    <artifactId>feign-form</artifactId>
                    <version>3.2.2</version>
                </dependency>

     

     注意,对于上传大文件,超过1M时,会提示文件过大。

    在配置文件中需要设置

    spring.servlet.multipart.enabled=true

    spring.servlet.multipart.max-file-size=10MB

    spring.servlet.multipart.max-request-size=10MB

     

    原来1.5.9配置为

    spring.http.multipart.enabled=true

    spring.http.multipart.max-file-size=10MB

    spring.http.multipart.max-request-size=10MB

     

    在使用Fegin上传时,不光要处理zuul的文件大小,文件超时问题,还要注意也要修改服务方的文件大小以及超时。

    
                                        
    联系QQ:343886028 --加QQ群 145315854