feign传参MultipartFile问题如何解决
发表于:2026-10-31 作者:千家信息网编辑
千家信息网最后更新 2026年10月31日,本篇内容介绍了"feign传参MultipartFile问题如何解决"的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,
千家信息网最后更新 2026年10月31日feign传参MultipartFile问题如何解决
本篇内容介绍了"feign传参MultipartFile问题如何解决"的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!
feign传参MultipartFile问题
首先,feign服务之间的调用,传参默认的格式为:ContentType=application/x-www-form-urlencoded
以表单的形式传参的,而文件流的传参,需要form-data的ContentType,否则会报错的
首先引入依赖
io.github.openfeign.form feign-form 3.8.0 io.github.openfeign.form feign-form-spring 3.8.0
注意spring boot版本是2.x以上的,上面两个依赖的版本不低于3.5.0,否则还是无效的
新建feign的配置
package com.wm.blog_config.config; import feign.codec.Encoder;import feign.form.spring.SpringFormEncoder;import org.springframework.beans.factory.ObjectFactory;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.autoconfigure.http.HttpMessageConverters;import org.springframework.cloud.openfeign.support.SpringEncoder;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration; /** * @author :半卷流年 * @description : 解决feign传递流数据的异常 * @createTime :2020/6/14 */@Configurationpublic class FeignSupportConfig { @Autowired private ObjectFactory messageConverters; @Bean public Encoder feignFormEncoder() { return new SpringFormEncoder(new SpringEncoder(messageConverters)); } } 在feign接口中配置
package com.wm.blog_admin.feign; import com.wm.blog_admin.feign.factory.PictureClientFallbackFactory;import com.wm.blog_common.constatnt.CommonConstant;import com.wm.blog_common.domain.TFileDO;import com.wm.blog_common.entity.TFile;import com.wm.blog_common.req.TFileQuery;import com.wm.blog_common.result.Result;import com.wm.blog_config.config.CustomFeignConfig;import com.wm.blog_config.config.FeignSupportConfig;import org.springframework.cloud.openfeign.FeignClient;import org.springframework.http.MediaType;import org.springframework.web.bind.annotation.*;import org.springframework.web.multipart.MultipartFile; import java.util.List; /*** * @ClassName: PictureFeignClient * @Description: picture feign调用 todo feign使用get有坑啊,是否考虑使用HttpClient替换feign的HttpURLConnection,采用apache的HttpClient * @Author: wm_yu * @Create_time: 16:39 2020-3-26 */@FeignClient(value = CommonConstant.PICTURE_MODULE_NAME, configuration = {CustomFeignConfig.class, FeignSupportConfig.class}, fallbackFactory = PictureClientFallbackFactory.class)public interface PictureFeignClient { /** * id查询图片信息 * @param id * @return */ @GetMapping("/web/picture/{id}") Result get(@PathVariable("id") Long id); /** * id批量查询图片信息 * @param idList * @return */ @PostMapping("/web/picture/getByIdList") Result> getByIdList(@RequestBody List idList); /** * 文件上传 * @param file * @return */ @PostMapping(value = "/web/picture/uploadFile",produces = {MediaType.APPLICATION_JSON_UTF8_VALUE},consumes = MediaType.MULTIPART_FORM_DATA_VALUE) Result uploadFile(@RequestPart("file") MultipartFile file); }
注意加上这个,表示传参格式:
就可以传参了的
Feign传输 MultipartFile的一些问题
File转MultipartFile
pom.xml
org.springframework spring-mock 2.0.8
public static MultipartFile getMultipartFile(String fileName, File file) throws IOException { return new MockMultipartFile(fileName, file.getName(), ContentType.APPLICATION_OCTET_STREAM.toString(), new FileInputStream(file));}报错 Current request is not a multipart request、Content type ‘’ not supported
@PostMapping设置 consumes = MediaType.MULTIPART_FORM_DATA_VALUE
使用@RequestPart(),不能使用@RequestParam()
@PostMapping(value = "/upload", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)ResultBody upload(@RequestPart(value = "file") MultipartFile file);
报错 Required request part ‘file’ is not present
configuration
@Configurationpublic class UploadFeignConfig { @Bean public Encoder multipartFormEncoder() { return new SpringFormEncoder(new SpringEncoder(new ObjectFactory() { @Override public HttpMessageConverters getObject() throws BeansException { return new HttpMessageConverters(new RestTemplate().getMessageConverters()); } })); }} FeignClient
@FeignClient(value = FileConstants.FILE_SERVER, configuration= UploadFeignConfig.class)public interface FileServiceClient extends IFileServiceClient { @Override @PostMapping(value = "/upload", consumes = MediaType.MULTIPART_FORM_DATA_VALUE) ResultBody upload(@RequestPart(value = "file") MultipartFile file);}"feign传参MultipartFile问题如何解决"的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注网站,小编将为大家输出更多高质量的实用文章!
问题
信息
内容
图片
文件
更多
格式
版本
知识
查询
配置
实用
学有所成
接下来
两个
之间
困境
实际
形式
情况
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
互联网科技公司简介
互联网科技工作委员会主任
极限挑战互联网科技
宜安科技 互联网金融
电子科技大学互联网
北京禾田雨橡互联网科技
科技伦理 互联网公司
互联网巨头投资的硬核科技
互联网公司会议桌创意科技软装饰
互联网实体化科技化
青海科技产业互联网发展
天津腾达互联网科技有限公司
互联网科技大赛获奖感言
群盈汇互联网科技有限公司
国大互联网教育科技有限公司薪资
互联网金融科技公司的优势
每天都有哪些好的互联网科技
同城互联网络科技
10位科技互联网大佬
互联网科技大佬云集
齐家互联网科技
狮子座互联网科技有限公司
郑州华宇互联网科技
阿里互联网科技公司
互联网金融中的现代科技
宜昌元轩互联网科技有限公司
从那以后互联网科技有限公司
南京一网一红互联网科技有限公司
保山互联网科技靠谱吗
第三届互联网大会安防黑科技