Springboot+ SpringCloud FeignClient发送MultipartFile

1.服务端

1.1 服务端(文件接收方)接口:

    @ApiOperation(value = "保存备份文件", notes = "保存备份文件", consumes = MediaType.MULTIPART_FORM_DATA_VALUE, produces = "application/json")
    @PostMapping(value = "/saveBackFile")
    @ResponseBody
    public Response saveBackFile(
            @ApiParam(value = "文件附加信息") @Valid BackupFileAttachInfo fileInfo,
            @ApiParam(value = "备份文件") @RequestPart("file") MultipartFile file);

1.2 服务端增加一个对象注入(可通过一个配置类的方法注入),配置文件上传临时目录

    @Bean
    public MultipartConfigElement multipartConfigElement() {
        MultipartConfigFactory factory = new MultipartConfigFactory();
        factory.setLocation("d:/"); //配置文件上传临时目录
        return factory.createMultipartConfig();
    }

2.客户端

2.0 SpringBoot启动类要增加  @EnableFeignClients 注解,否则,FeignClient不会生效!

2.1 首先pom中要增加feign和eureka等的依赖,还要加入下面的依赖:

       <!--添加Springboot对MultipartFile的类库支持,在spring-test包中-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>5.1.3.RELEASE</version>
            <scope>compile</scope>
        </dependency>
        <!--spring feign form 表单提交相关-->
        <dependency>
            <groupId>io.github.openfeign.form</groupId>
            <artifactId>feign-form-spring</artifactId>
            <version>3.4.1</version>
        </dependency>
        <!--feign form 表单提交相关-->
        <dependency>
            <groupId>io.github.openfeign.form</groupId>
            <artifactId>feign-form</artifactId>
            <version>3.4.1</version>
        </dependency>
        <!--文件上传相关依赖-->
        <dependency>
            <groupId>commons-fileupload</groupId>
            <artifactId>commons-fileupload</artifactId>
            <version>1.3.3</version>
        </dependency>

2.2 FeignClient

@Service
@FeignClient(name = "MODEL-LIB",value="MODEL-LIB" , configuration = FeignMultipartSupportConfig.class,url="http://127.0.0.1:8080/file") 
public interface ModellibFeignClient {
    @PostMapping(value = "/saveBackFile" ,consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    @ResponseBody
    public BaseResponsev1 saveBackFile(
            @ApiParam(value = "租户id") @RequestParam("tenant") String tenant,
            @ApiParam(value = "project") @RequestParam("project") String project,
            @ApiParam(value = "namespace") @RequestParam("namespace") String namespace,
            @ApiParam(value = "username") @RequestParam("username") String username,
            @ApiParam(value = "备份文件")  @RequestPart("file")  MultipartFile file);
}

2.3 Feign配置类,主要是对MultipartFile上传时的编码相关:FeignMultipartSupportConfig,如果不配置这个类,默认会将文件也编译成json,导致报错:

@Configuration
public class FeignMultipartSupportConfig {
    @Autowired
    private ObjectFactory<HttpMessageConverters> messageConverters;

    @Bean
    @Primary
    @Scope("prototype")
    public Encoder multipartFormEncoder() {
        return new SpringFormEncoder(new SpringEncoder(messageConverters));
    }

    @Bean
    public feign.Logger.Level multipartLoggerLevel() {
        return Logger.Level.FULL;
    }
}

2.4 调用FeignClient

	File file = new File("d://lq.jpg");

	MultipartFile multipartFile = null;

	DiskFileItem fileItem = (DiskFileItem) new DiskFileItemFactory().createItem("file", //这个file对应feignClient中的MultipartFile参数名,写错就会报400 :"message":"Required request part 'file' is not present"这个错误,切记!
	        MediaType.MULTIPART_FORM_DATA_VALUE, true, file.getName());

	try (InputStream input = new FileInputStream(file); OutputStream os = fileItem.getOutputStream()) {
	    IOUtils.copy(input, os);
	} catch (Exception e) {
	    throw new IllegalArgumentException("Invalid file: " + e, e);
	}

	multipartFile = new CommonsMultipartFile(fileItem);

	System.out.println(multipartFile);
	BaseResponsev1 response = modellibFeignClient.saveBackFile("lq", "zwr", "zhh", "zhh1", multipartFile);

完全照此步骤操作,可保证FeignClient上传MultipartFile成功!

至此完成!

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值