springcloud如何使用Feign后台内部传递MultipartFile

 更新时间:2022年03月04日 11:52:51   作者:linli1991  
这篇文章主要介绍了springcloud如何使用Feign后台内部传递MultipartFile,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教

如何使用Feign后台内部传递MultipartFile

先修改Feign Client接口

 
import feign.codec.Encoder;
import feign.form.spring.SpringFormEncoder;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.context.annotation.Bean;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.multipart.MultipartFile;
 
/**
 * @author linli
 * @date 20-06-27
 */
@FeignClient(value = "upload", fallbackFactory = UploadFallbackFactory.class, configuration = UploadClient.MultipartSupportConfig.class)
public interface UploadClient {
 
    @PostMapping(path = "/upload-text", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    String uploadText(@RequestPart(name = "file") MultipartFile file);
 
    /**
     * 引用配置类MultipartSupportConfig.并且实例化
     */
    class MultipartSupportConfig {
        @Bean
        public Encoder feignFormEncoder() {
            return new SpringFormEncoder();
        }
    }
}

若SpringFormEncoder 引入报错,加上下面的依赖

<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>

内部调用

private String uploadFile(String str) {
        FileOutputStream fos = null;
        FileInputStream fis = null;
        MultipartFile multipartFile = null;
        byte[] bt = str.getBytes();
        File file = null;
        try {
            file = File.createTempFile("file" + UUID.randomUUID(), ".txt");
            fos = new FileOutputStream(file);
            fos.write(bt, 0, bt.length);
            fis = new FileInputStream(file);
            multipartFile = new MockMultipartFile("file", file.getName(),
                    MediaType.TEXT_PLAIN_VALUE, fis);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fis != null) {
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return uploadClient.uploadText(multipartFile);
    }

注意点

Feign进行跨服务传递MultipartFile文件

通过调用服务进行文件上传,避免每个需要上传文件的模块都写一遍上传服务,造成代码冗余。

本文主要包含通过feign进行文件上传模块。

使技术人员在开发过程中遇到问题时有地可查,有章可循。

通过feign进行跨服务传递MultipartFile文件

添加依赖

<dependency>
   <groupId>io.github.openfeign.form</groupId>
   <artifactId>feign-form</artifactId>
   <version>3.0.3</version>
</dependency>
<dependency>
   <groupId>io.github.openfeign.form</groupId>
   <artifactId>feign-form-spring</artifactId>
   <version>3.0.3</version>
</dependency>

添加配置文件

package com.ruiyi.twowayreferral.configurer;
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;
@Configuration
public class MultipartSupportConfig {
   @Autowired
   private ObjectFactory<HttpMessageConverters> messageConverters;
   @Bean
   public Encoder feignFormEncoder() {
       return new SpringFormEncoder(new SpringEncoder(messageConverters));
   }
}

代码示例

@FeignClient(value = "controller-center")
public interface CallFrignService {
    /**
     * @Create 文件上传 wanggx_ruiyi 2019.11.15
     * @param uploadPath 文件上传地址
     * @param file 上传的文件
     * @return
     */
   @PostMapping(value = "/api/v1/common/file/fileUpload",consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
   String fileUpload(@RequestParam(value = "uploadPath", required = true) String uploadPath,@RequestPart(value = "file", required = true) MultipartFile file);
}

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

相关文章

  • 只需两步实现Eclipse+Maven快速构建第一个Spring Boot项目

    只需两步实现Eclipse+Maven快速构建第一个Spring Boot项目

    这篇文章主要介绍了只需两步实现Eclipse+Maven快速构建第一个Spring Boot项目,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-12-12
  • Java Spring 循环依赖解析

    Java Spring 循环依赖解析

    这篇文章主要介绍了Java Spring 循环依赖解析,Spring 现在其实是我们 Java 程序开发离不开的基础框架,个人觉得除了 JDK 我们用得最多的 Java 中间件就是 Spring ,今天我们一起来学习一下 Spring 的循环依赖。下面详细内容需要的小伙伴可以参考一下
    2022-02-02
  • SpringBoot实现定时任务的三种方式小结

    SpringBoot实现定时任务的三种方式小结

    这篇文章主要介绍了SpringBoot实现定时任务的三种方式小结,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-11-11
  • Spring 框架的 MethodInterceptor 简介及示例代码

    Spring 框架的 MethodInterceptor 简介及示例代码

    MethodInterceptor接口定义了一个方法Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) ,该方法在代理对象的方法被调用时被触发,这篇文章主要介绍了Spring 框架的 MethodInterceptor 简介及示例代码,需要的朋友可以参考下
    2023-09-09
  • Java中sharding-jdbc按年月分片的示例代码

    Java中sharding-jdbc按年月分片的示例代码

    本文主要介绍了Java中sharding-jdbc按年月分片的示例代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-07-07
  • 如何利用 Either 和 Option 进行函数式错误处理

    如何利用 Either 和 Option 进行函数式错误处理

    这篇文章主要介绍了如何利用 Either 和 Option 进行函数式错误处理。在 Java 中,错误的处理在传统上由异常以及创建和传播异常的语言支持进行。但是,如果不存在结构化异常处理又如何呢?,需要的朋友可以参考下
    2019-06-06
  • SpringBoot整合Mybatis实现商品评分的项目实践

    SpringBoot整合Mybatis实现商品评分的项目实践

    本文介绍了SpringBoot整合Mybatis-plus框架实现对商品评分的功能实现流程和前端接口实现过程,文中通过示例代码介绍的非常详细,需要的朋友们下面随着小编来一起学习学习吧
    2024-02-02
  • Spring Validation接口入参校验示例代码

    Spring Validation接口入参校验示例代码

    Spring Validation是一种用于实现数据校验的框架,它提供了一系列的校验器,针对不同的数据类型可以使用不同的校验器进行校验,下面这篇文章主要给大家介绍了关于Spring Validation接口入参校验的相关资料,需要的朋友可以参考下
    2023-06-06
  • JAVA中DIFF算法实现

    JAVA中DIFF算法实现

    本文主要介绍了JAVA中DIFF算法实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-07-07
  • Java自动拆箱空指针异常的解决

    Java自动拆箱空指针异常的解决

    这篇文章主要介绍了Java自动拆箱空指针异常的解决,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-03-03

最新评论