springcloud微服务之间通过feign调用方式上传文件MultipartFile

通过我们使用spring系列的框架上传文件,都会利用MultipartFile文件流进行文件的上传。但是我在最近的项目中遇到一个问题,我们的项目是基于springboot框架的springcloud微服务搭建的,后端服务技术层面整体上分为business服务和core服务,business服务用于作为应用层,直接连接用户端,通常用于聚合数据,core服务用于基本很少变动的非业务核心接口。那么用户的上传过程实现,就要通过前端页面调用business服务上传接口,然后business服务调用core服务的上传接口实现文件上传。在服务与服务之间通过feign调用时,发现MultipartFile文件流上传文件会报错。经过不断摸索以及网上的方案。最终解决了,现在把问题方案贴出来,希望能帮到有需要的开发者朋友。

一、编写配置类

编写一个配置类,也就是把multipart/form-data格式的文件流编码为spring框架能够解析的格式

@Configuration
public class MultipartSupportConfig {

    @Autowired
    private ObjectFactory<HttpMessageConverters> messageConverters;
 
    @Bean
    public Encoder feignFormEncoder() {
        return new SpringFormEncoder(new SpringEncoder(messageConverters));
    }
}

二、服务调用方加注解类

/**
 * @author guobinhui
 * @date 2019/10/31
 */
@FeignClient(name="xxxx", configuration = MultipartSupportConfig.class)
public interface CreditEvaluationClient {
 
    @ApiOperation(value = "信用评估上传营业执照", notes = "信用评估上传营业执照")
    @PostMapping(value="/custevaluation/uploadFile",consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public DataResponseEntity<?> uploadFile(@RequestPart("file") MultipartFile multipartFile,@RequestParam("custId") String custId)throws FastDfsException;

三、被调用的服务接收文件流

    @ApiOperation(value = "信用评估上传营业执照", notes = "信用评估上传营业执照")
    @PostMapping("/uploadFile")
    public ResponseEntity <?> uploadFile(@RequestPart("file") MultipartFile multipartFile,@RequestParam("custId") String custId)throws FastDfsException{
        String fileName = multipartFile.getOriginalFilename();
        String ext = fileName.substring(fileName.indexOf(".")+1, fileName.length());
        if(!ext.equals("pdf") && !ext.equals("jpg") && !ext.equals("png")){
            return ResponseHelper.fail(MsConstant.ILLEGAL_FILE_EXT);
        }
        String path = custEvalService.uploadFile(multipartFile,custId);
        return ResponseHelper.success(path);
    }

注意:文件流要用RequestPart注解传参,其余参数用RequestParam注解传参

  • 4
    点赞
  • 40
    收藏
    觉得还不错? 一键收藏
  • 9
    评论
在Spring Cloud微服务架构中,Nacos是一个注册中心和配置中心。Feign是一个声明式的Web服务客户端,它使得编写Web服务客户端变得更加容易。 使用Feign调用接口需要以下步骤: 1. 在pom.xml中添加Feign依赖 ```xml <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency> ``` 2. 在启动类上添加@EnableFeignClients注解启用Feign客户端 ```java @SpringBootApplication @EnableDiscoveryClient @EnableFeignClients public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } } ``` 3. 创建接口,并使用@FeignClient注解指定调用的服务名称和服务路径 ```java @FeignClient(name = "service-provider") public interface UserService { @GetMapping("/user/{id}") String getUserById(@PathVariable("id") Long id); } ``` 其中,name属性指定服务名称,GetMapping注解指定服务路径。 4. 在需要使用该服务的地方注入UserService并调用方法即可 ```java @RestController public class UserController { @Autowired private UserService userService; @GetMapping("/user/{id}") public String getUserById(@PathVariable("id") Long id) { return userService.getUserById(id); } } ``` 在这个例子中,我们定义了一个名为UserService的Feign客户端,指定了调用的服务名称和服务路径。然后在UserController中注入了UserService并调用了其方法。最终,Feign会自动将该请求转发到名为service-provider的微服务,并返回结果。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值