Spring Cloud Feign的文件上传实现

福利持续进行中,免费加入知识星球和斐讯K3抽奖等你来!!!


在Spring Cloud封装的Feign中并不直接支持传文件,但可以通过引入Feign的扩展包来实现,本文就来具体说说如何实现。

服务提供方(接收文件)

服务提供方的实现比较简单,就按Spring MVC的正常实现方式即可,比如:

  
  
  1. @EnableFeignClients

  2. @EnableDiscoveryClient

  3. @SpringBootApplication

  4. public class Application {

  5.    @RestController

  6.    public class UploadController {

  7.        @PostMapping(value = "/uploadFile", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)

  8.        public String handleFileUpload(@RequestPart(value = "file") MultipartFile file) {

  9.            return file.getName();

  10.        }

  11.    }

  12.    public static void main(String[] args) {

  13.        new SpringApplicationBuilder(Application.class).web(true).run(args);

  14.    }

  15. }

服务消费方(发送文件)

在服务消费方由于会使用Feign客户端,所以在这里需要在引入feign对表单提交的依赖,具体如下:

  
  
  1. <dependency>

  2.   <groupId>io.github.openfeign.form</groupId>

  3.   <artifactId>feign-form</artifactId>

  4.   <version>3.0.3</version>

  5. </dependency>

  6. <dependency>

  7.   <groupId>io.github.openfeign.form</groupId>

  8.   <artifactId>feign-form-spring</artifactId>

  9.   <version>3.0.3</version>

  10. </dependency>

  11. <dependency>

  12.   <groupId>commons-fileupload</groupId>

  13.   <artifactId>commons-fileupload</artifactId>

  14.   <version>1.3.3</version>

  15. </dependency>

定义文件上传方的应用主类和FeignClient,假设服务提供方的服务名为 eureka-feign-upload-server

  
  
  1. @EnableFeignClients

  2. @EnableDiscoveryClient

  3. @SpringBootApplication

  4. public class Application {

  5.    public static void main(String[] args) {

  6.        new SpringApplicationBuilder(Application.class).web(true).run(args);

  7.    }

  8. }

  9. @FeignClient(value = "upload-server", configuration = UploadService.MultipartSupportConfig.class)

  10. public interface UploadService {

  11.    @PostMapping(value = "/uploadFile", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)

  12.    String handleFileUpload(@RequestPart(value = "file") MultipartFile file);

  13.    @Configuration

  14.    class MultipartSupportConfig {

  15.        @Bean

  16.        public Encoder feignFormEncoder() {

  17.            return new SpringFormEncoder();

  18.        }

  19.    }

  20. }

在启动了服务提供方之后,尝试在服务消费方编写测试用例来通过上面定义的Feign客户端来传文件,比如:

  
  
  1. @Slf4j

  2. @RunWith(SpringJUnit4ClassRunner.class)

  3. @SpringBootTest

  4. public class UploadTester {

  5.    @Autowired

  6.    private UploadService uploadService;

  7.    @Test

  8.    @SneakyThrows

  9.    public void testHandleFileUpload() {

  10.        File file = new File("upload.txt");

  11.        DiskFileItem fileItem = (DiskFileItem) new DiskFileItemFactory().createItem("file",

  12.                MediaType.TEXT_PLAIN_VALUE, true, file.getName());

  13.        try (InputStream input = new FileInputStream(file); OutputStream os = fileItem.getOutputStream()) {

  14.            IOUtils.copy(input, os);

  15.        } catch (Exception e) {

  16.            throw new IllegalArgumentException("Invalid file: " + e, e);

  17.        }

  18.        MultipartFile multi = new CommonsMultipartFile(fileItem);

  19.        log.info(uploadService.handleFileUpload(multi));

  20.    }

  21. }

完整示例:

读者可以根据喜好选择下面的两个仓库中查看 eureka-feign-upload-servereureka-feign-upload-client两个项目:

  • Github:https://github.com/dyc87112/SpringCloud-Learning/

  • Gitee:https://gitee.com/didispace/SpringCloud-Learning/

如果您对这些感兴趣,欢迎star、follow、收藏、转发给予支持!

福利持续进行中,免费加入知识星球和斐讯K3抽奖等你来!!!

推荐阅读


长按指纹

一键关注

深入交流、更多福利

扫码加入我的知识星球



点击 “阅读原文” 看看本号其他精彩内容

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值