springcloud使用Feign后台内部传递MultipartFile

 1.先修改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>

2.内部调用

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);
    }

3.注意点

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Cloud使用Feign是一种声明式的Web服务客户端,它简化了使用HTTP客户端进行远程服务调用的过程。Feign内部集成了Ribbon负载均衡器和Hystrix断路器,可以实现服务的负载均衡和容错处理。 要使用Feign,首先需要在项目中添加相应的依赖。在Maven项目中,可以添加以下依赖: ```xml <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency> ``` 然后,在启动类上加上@EnableFeignClients注解来启用Feign客户端: ```java @SpringBootApplication @EnableFeignClients public class YourApplication { public static void main(String[] args) { SpringApplication.run(YourApplication.class, args); } } ``` 接下来,定义一个Feign客户端接口,使用@FeignClient注解指定要调用的服务名,并定义接口中的方法与远程服务的API接口相对应。例如: ```java @FeignClient(name = "your-service") public interface YourServiceClient { @GetMapping("/api/something") String getSomething(); } ``` 在需要调用远程服务的地方,可以通过注入此Feign客户端接口来进行调用: ```java @RestController public class YourController { private final YourServiceClient yourServiceClient; public YourController(YourServiceClient yourServiceClient) { this.yourServiceClient = yourServiceClient; } @GetMapping("/something") public String getSomething() { return yourServiceClient.getSomething(); } } ``` 以上是使用Feign进行远程服务调用的基本步骤,通过Feign可以方便地实现服务之间的通信。同时,Feign还支持对请求参数、请求头、请求体等进行配置和传递,以满足各种复杂的场景需求。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值