Spring Cloud Feign如何上传文件

去查看原文
1、Feign官方提供了feign 的子项目feign-fromjava

一、加依赖git

io.github.openfeign.form feign-form 3.0.3 io.github.openfeign.form feign-form-spring 3.0.3
java开发工具下载地址及安装教程大全,点这里
更多深度技术文章,在这里github

二、编写Feign Client
@FeignClient(name = “ms-content-sample”, configuration = UploadFeignClient.MultipartSupportConfig.class)
public interface UploadFeignClient {
@RequestMapping(value = “/upload”, method = RequestMethod.POST,
produces = {MediaType.APPLICATION_JSON_UTF8_VALUE},
consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
@ResponseBody
String handleFileUpload(@RequestPart(value = “file”) MultipartFile file);
class MultipartSupportConfig {
@Bean public Encoder feignFormEncoder() {
return new SpringFormEncoder();
}
}
}
如代码所示,在这个Feign Client中,咱们引用了配置类MultipartSupportConfig ,在MultipartSupportConfig 中,咱们实例化了
SpringFormEncoder 。这样这个Feign Client就可以上传了
注意:spring

@RequestMapping(value = “/upload”, method = RequestMethod.POST,
produces = {MediaType.APPLICATION_JSON_UTF8_VALUE},
consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
(1)、produeces 、consumes 不能少app

(2)、接口定义中的注解@RequestPart(value = “file”) 不能写成@RequestParam(value = “file”工具

(3)、最好将Hystrix的超时时间设长一点,例如5秒,不然可能文件还没上传完,Hystrix就超时了,从而致使客户端侧的报错开发工具

原创文章,转载请注明出处。
java开发工具下载地址及安装教程大全,点这里
更多深度技术文章,在这里code