SpringCloud 使用feign进行文件MultipartFile传输
SpringCloud组件fiegn默认是不支持传递文件的。但是提供了feign-form扩展工具
解决方法:
步骤一:在消费者服务中加入相关pom依赖。
<!--解决SpringCloud 组件feign默认是不支持传递文件的--> <dependency> <groupId>io.github.openfeign.form</groupId> <artifactId>feign-form-spring</artifactId> <version>3.6.0</version> </dependency> <dependency> <groupId>io.github.openfeign.form</groupId> <artifactId>feign-form</artifactId> <version>3.6.0</version> </dependency> <dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> <version>1.3.3</version> </dependency>
步骤二:编写一个配置类
package com.ckfuture.springcloud.config; import feign.Logger; 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; import org.springframework.context.annotation.Primary; import org.springframework.context.annotation.Scope; /** * 解决feign文件传递问题的配置类 */ @Configuration public class FeignMultipartSupportConfig { @Autowired private ObjectFactory<HttpMessageConverters> messageConverters; @Bean @Primary @Scope("prototype") public Encoder multipartFormEncoder(){ return new SpringFormEncoder(new SpringEncoder(messageConverters)); } @Bean public feign.Logger.Level multipartLoggerLevel(){ return feign.Logger.Level.FULL; } }
步骤三:feign接口的编写。服务调用方加注解类(配置类)
package com.ckfuture.springcloud.deliveryman.service; import com.alibaba.fastjson.JSONObject; import com.ckfuture.springcloud.config.FeignMultipartSupportConfig; import com.ckfuture.springcloud.utils.Result; import org.springframework.cloud.openfeign.FeignClient; import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; import java.io.IOException; /** * @description: 消费者-信息服务接口 * @author: CKFuture * @since: 2024-03-19 13:24 * @version: v1.0 * @LastEditTime: * @LastEditors: * @copyright: hrbckfuture.com */ @FeignClient(value = "${service-url.nacos-user-service}",configuration = FeignMultipartSupportConfig.class)//解决feign文件传递问题的配置类feign配置类 public interface IDeliverymanService { /** * 批量添加信息 * @param excelFile * @param agencyId * @return */ @PostMapping(value = "/deliveryman/uploadExcel", consumes = {"multipart/form-data"})//“multipart/form-data”指定类型 public Result uploadExcel( @RequestPart("excelFile") MultipartFile excelFile, @RequestParam(value = "agencyId", required = true) Integer agencyId ) throws IOException; }
步骤四:修改消费者提供的服务提供类@RequestPart 来接收文件参数
package com.ckfuture.springcloud.deliveryman.controller; import com.alibaba.fastjson.JSONObject; import com.ckfuture.springcloud.deliveryman.service.IDeliverymanService; import com.ckfuture.springcloud.utils.Result; import io.swagger.annotations.Api; import org.springframework.core.io.ByteArrayResource; import org.springframework.http.HttpEntity; import org.springframework.http.HttpHeaders; import org.springframework.http.ResponseEntity; import org.springframework.util.LinkedMultiValueMap; import org.springframework.util.MultiValueMap; import org.springframework.web.bind.annotation.*; import org.springframework.web.client.RestTemplate; import org.springframework.web.multipart.MultipartFile; import javax.annotation.Resource; import org.springframework.http.MediaType; import java.io.IOException; /** * @descrption: 消费者-信息控制器 * @author: CKFuture * @since: 2024-03-19 13:35 * @version: v1.0 * @LastEditTime: * @LastEditors: * @copyright: hrbckfuture.com */ @Api(tags={"基础管理/信息接口"}) @RestController public class DeliverymanController { @Resource private IDeliverymanService service; /** * 批量添加信息 * @param excelFile * @param agencyId * @return */ @PostMapping(value = "/deliveryman/uploadExcel",consumes = MediaType.MULTIPART_FORM_DATA_VALUE) public Result uploadExcel( @RequestPart("excelFile") MultipartFile excelFile, @RequestParam(value = "agencyId", required = true) Integer agencyId ){ try{ System.out.println("我被调用了!"); return service.uploadExcel(excelFile,agencyId); }catch (Exception ex){ System.out.println(ex.getMessage()); return Result.failure(1,"error"); } } }
注意点:文件流要用RequestPart注解传参数,其余参数用RequestParam注解传参。
步骤五:生产者提供服务
/** * 生产者-批量添加信息 * @param excelFile * @param agencyId * @return * @throws IOException */ @PostMapping(value = "/deliveryman/uploadExcel", consumes = {"multipart/form-data"}) @ResponseBody public Result uploadExcel( @RequestParam("excelFile") MultipartFile excelFile, @RequestParam(value = "agencyId", required = true) Integer agencyId ) throws IOException { try { //逻辑代码 } catch (Exception ex) { return Result.failure(ResponseCode.ERROR, ResponseMsg.QUERY_ERROR); } }
本文引用:https://blog.csdn.net/qq_52211542/article/details/129779344
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!