在Spring Cloud封装的Feign中并不直接支持传文件,但Feign官方提供了子项目Feign-form来实现跨服务的文件上传
服务提供方(接收文件)
服务提供方的实现比较简单,就按Spring MVC的正常实现方式即可,比如:
@PostMapping(value = "/fileupload", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public String fileupload(@RequestPart("file")MultipartFile file){
if(file == null){
return "附件为空,上传文件失败";
}
String filename = file.getOriginalFilename();
long fileSize = file.getSize();
return "上传成功,文件名为:"+filename+" 文件大小:"+fileSize;
}
服务消费方(发送文件)
在服务消费方由于会使用Feign客户端,所以在这里需要在引入feign对表单提交的依赖,具体如下:
<dependency>
<groupId>io.github.openfeign.form</groupId>
<artifactId>feign-form</artifactId>
<version>3.0.3</version>
</dependency>
<dependency>
<groupId>io.github.openfeign.form</groupId>
<artifactId>feign-form-spring</artifactId>
<version>3.0.3</version>
</dependency>
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.3</version>
</dependency>
定义文件上传方的应用主类和FeignClient,假设服务提供方的服务名为MICROSERVICE-PROVIDER-USER
@FeignClient(value = "MICROSERVICE-PROVIDER-USER", configuration = {UserService.MultipartSupportConfig.class})
public interface UserService {
@PostMapping(value = "/user/fileupload",
consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public String fileupload(@RequestPart("file") MultipartFile file);
@Configuration
class MultipartSupportConfig {
@Bean
public Encoder feignFormEncoder() {
return new SpringFormEncoder();
}
}
}
服务消费端接口,就按Spring MVC的正常实现方式即可,比如:
@RequestMapping("/user")
@RestController
public class UserController {
@Autowired
private UserService userService;
@PostMapping(value = "/fileupload")
public String fileupload(@RequestPart("file") MultipartFile file){
return userService.fileupload(file);
}
}
测试结果:
注意:
- 接口定义中的注解@RequestPart(“file”)不能写成@RequestParam("file")
- 最好将Hystrix的超时时间设置的长一些,否则文件还没有上传完,Hystrix就超时,导致文件上传失败