SpringCloud---Feign上传下载详解

technologykai 2018-07-19 原文

SpringCloud—Feign上传下载详解

1.使用原因

 公司最近做的项目在用SpringCloud,设计到了上传。但是Feign本身是不支持文件类型的。所以这里把上传下载的实现分享一下。

2.所需配置

  这是自己实现的一个formEncoder,可以支持单文件和数组的多文件上传

public class FeignSpringFormEncoder extends FormEncoder {

/**
* Constructor with the default Feign's encoder as a delegate.
*/
public FeignSpringFormEncoder() {
this(new Default());
}


/**
* Constructor with specified delegate encoder.
*
* @param delegate delegate encoder, if this encoder couldn't encode object.
*/
public FeignSpringFormEncoder(Encoder delegate) {
super(delegate);

MultipartFormContentProcessor processor = (MultipartFormContentProcessor) getContentProcessor(ContentType.MULTIPART);
processor.addWriter(new SpringSingleMultipartFileWriter());
processor.addWriter(new SpringManyMultipartFilesWriter());
}


@Override
public void encode(Object object, Type bodyType, RequestTemplate template) throws EncodeException {
if (bodyType.equals(MultipartFile.class)) {
MultipartFile file = (MultipartFile) object;
Map data = Collections.singletonMap(file.getName(), object);
super.encode(data, MAP_STRING_WILDCARD, template);
return;
} else if (bodyType.equals(MultipartFile[].class)) {
MultipartFile[] file = (MultipartFile[]) object;
if(file != null) {
Map data = Collections.singletonMap(file.length == 0 ? "" : file[0].getName(), object);
super.encode(data, MAP_STRING_WILDCARD, template);
return;
}
}
super.encode(object, bodyType, template);
}
}

将实现的类注册一下。
@Bean
public Encoder feignEncoder(ObjectFactory<HttpMessageConverters> messageConverters) {
return new FeignSpringFormEncoder(new SpringEncoder(messageConverters));
}


调用方的代码,这里参数接收的时候用的是@RequestPart,与@RequestParam区别大家可以去查一下。
@PostMapping(value = "/upload", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
@ResponseBody
public ApiResult upload(@RequestPart(value = "file") MultipartFile file) {
return fileUploadApiClient.upload(file);
}

暴露的fileUploadApiClient接口还需要添加依赖
<dependency>
<groupId>io.github.openfeign.form</groupId>
<artifactId>feign-form</artifactId>
<version>3.3.0</version>
</dependency>
<dependency>
<groupId>io.github.openfeign.form</groupId>
<artifactId>feign-form-spring</artifactId>
<version>3.3.0</version>
</dependency>

   暴露的fileUploadApiClient代码,MediaType类型的指定

@PostMapping(value = "/oss/upload", consumes = MediaType.MULTIPART_FORM_DATA_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
ApiResult upload(@RequestPart(value = "file") MultipartFile file);

最后直接调用就可以上传成功.
发表于 2018-07-19 12:01 问题大白 阅读() 评论() 编辑 收藏

 

版权声明:本文为technologykai原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/technologykai/p/9335029.html

SpringCloud---Feign上传下载详解的更多相关文章

  1. Spring Boot(2)中的yaml配置简介

        搞Spring Boot的小伙伴都知道,Spring Boot中的配置文件有两种格式,properti […]...

  2. 大白话spring依赖注入

    在前边的文章中分享了spring如何实现属性的注入,有注解和配置文件两种方式,通过这两种方式可以实现sprin […]...

  3. Spring源码分析(一)–BeanProcessor

    一、何谓BeanProcessor   BeanProcessor是SpringFramework里非常重要的 […]...

  4. spring boot 与 thymeleaf (1): 国际化

    在thymeleaf 里面有个消息表达式: #{…} , 可以借此来实现国际化.  在我使用这个功 […]...

  5. 最全面阐述WebDataBinder理解Spring的数据绑定

    每篇一句 不要总问低级的问题,这样的人要么懒,不愿意上网搜索,要么笨,一点独立思考的能力都没有 相关阅读 【小 […]...

  6. springboot spring security 覆盖默认登录Filter, Session manage

          pom.xml <?xml version="1.0" encoding="UTF-8"?& […]...

  7. Spring Cloud Alibaba Nacos

    一、介绍   Nacos:由单词Naming和Cofiguration的前两个字母组成,最后的s代表Servi […]...

  8. 看完就会的Spring Cloud Gateway

    在前面几节,我给大家介绍了当一个系统拆分成微服务后,会产生的问题与解决方案:服务如何发现与管理(Nacos注册 […]...

随机推荐

  1. 一招详解如何使用数据可视化BI软件创建销售数据统计大屏

    灯果数据可视化BI软件是新一代人工智能数据可视化大屏软件,内置丰富的大屏模板,可视化编辑操作,无需任何经验就可 […]...

  2. java环境变量设置(完美版) – java环境变量

    java环境变量设置(完美版) 2012-08-15 11:11  java环境变量  阅读(256)  评论 […]...

  3. 三层架构教程举例

    三层架构:只说明较好,不是非用不可!(1) 2012-09-28 13:43 浪漫骑士必胜 博客园 我要评论( […]...

  4. POJ 2398 Toy Storage 二分+叉积

    Description   Mom and dad have a problem: their child, […]...

  5. Echart – 最好最强大效果最丰富的可视化图表插件

    # 官网http://echarts.baidu.com/# demohttp://echarts.baidu […]...

  6. Windows 2000实现网络共享连接 – surfer

    Windows 2000实现网络共享连接 在微软以前版本的视窗操作系统中,我们实现共享连接一般使用WinGat […]...

  7. EXADATA智能扫描

    EXADATA 智能扫描 索引快速全扫描和索引全扫描 提要:查询特定的要求:智能扫描只可用于完整的表或索引扫描 […]...

  8. fetch()函数使用的一些技巧

    最近项目用到了一些es6的知识,其中大篇幅在vue框架中使用了fetch()函数,总结了一些使用的技巧: 一, […]...

展开目录

目录导航