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.spring work</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 Responsev1 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 Factory<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);
Responsev1 response = modellibFeignClient.saveBackFile(\"lq\", \"zwr\", \"zhh\", \"zhh1\", multipartFile);
完全照此步骤操作,可保证FeignClient上传MultipartFile成功!
至此完成!
继续阅读与本文标签相同的文章
-
8 分钟了解 Kubernetes
2026-05-18栏目: 教程
-
Helm 从入门到实践 | 从 0 开始制作一个 Helm Charts
2026-05-18栏目: 教程
-
阿里云突发性能实例t5 和共享型实例xn4 n4的区别
2026-05-18栏目: 教程
-
【DockerCon2017技术解读】Docker特性介绍
2026-05-18栏目: 教程
-
面向海量数据的极致成本优化-云HBase的一体化冷热分离
2026-05-18栏目: 教程
