美文网首页
Feign 如何支持进行文件上传

Feign 如何支持进行文件上传

作者: 程序员小杰 | 来源:发表于2020-03-21 21:37 被阅读0次

嗨咯大家好,好久没写了,疫情期间在家也变懒了,不想打开电脑,不想学习敲代码。
不过今天在家上了一天班,遇到了一个问题,弄的我解决了四个小时。当然是百度四个小时,哈哈。不说了,进入正题。

一、依赖

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

我这里的feign是9的版本,所以依赖使用的是3.5之前的。如果要使用3.5之后的,feign需要是10之后的版本。


image.png image.png

官网地址:https://github.com/OpenFeign/feign-form

二、创建FeignConfig(重要)

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.context.annotation.Scope;

import feign.codec.Encoder;
import feign.form.spring.SpringFormEncoder;

@Configuration
public class FeignConfig {
    
    @Bean
    @Primary
    @Scope("prototype")
    public Encoder multipartFormEncoder() {
        return new SpringFormEncoder();
    }

    @Bean
    public feign.Logger.Level multipartLoggerLevel() {
        return feign.Logger.Level.FULL;
    }
}

三、创建Client

@FeignClient(name = "需要对接的服务名",configuration = FeignConfig.class)
@Component
public interface UploadFileClient{

@PostMapping(value="/api/gongj/uploadFile",produces = MediaType.APPLICATION_JSON_UTF8_VALUE, 
            consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    String uploadFile(@RequestPart("multipartFile") MultipartFile multipartFile,@RequestParam("title") String title);
}

四、Controller

    @PostMapping("/uploadFile")
    public ResultVo<String> uploadFile(@RequestParam("title") String title){
        String result = xxxService.uploadFile(title);
        return result;
    }

五、Service

        @Autowired
    UploadFileClient uploadFileClient;

public ResultVo<String> uploadFile(String title) {
            File file = new File("D:\\ee\\审核表.pdf");
        MultipartFile multipartFile = FileUtil.fileToMultipartFile(file);
        String result = uploadFileClient.uploadFile(multipartFile, title);
        return result;
    }

由于我们这块文件是由后台产生的,所以需要将File转换为MultipartFile。如果是前台传入的,就不需要转了。

六、加入新依赖

<dependency>
            <groupId>commons-fileupload</groupId>
            <artifactId>commons-fileupload</artifactId>
            <version>1.3.3</version>
        </dependency>

FileUtils

public static MultipartFile fileToMultipartFile(File file) {
        String fieldName = "multipartFile";  //重点  这个名字需要和你对接的MultipartFil的名称一样
        FileItemFactory factory = new DiskFileItemFactory(16, null);
        FileItem item = factory.createItem(fieldName, "multipart/form-data", true, file.getName());
        int bytesRead = 0;
        byte[] buffer = new byte[8192];
        try {
            FileInputStream fis = new FileInputStream(file);
            OutputStream os = item.getOutputStream();
            while ((bytesRead = fis.read(buffer, 0, 8192)) != -1) {
                os.write(buffer, 0, bytesRead);
            }
            os.close();
            fis.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return new CommonsMultipartFile(item);
    }

这是服务端的代码。


image.png

这是FeignClient的代码。


image.png
这几个地方一定要保持一致,就是这个地方我找了好久。不然会报

Required request part ‘file’ is not present。

补充:

写完之后测试了一下的确支持文件传输了,以为大功告成。不过今天又写了一个接口,需要传递实体的。出现了

is not a type supported by this encoder

又开始百度一番,原来是冲突了,上面那种解决方式不太完美。
参考:https://blog.csdn.net/gududedabai/article/details/79895893
上面博文的方案一就不用试了,那种只适用于只有文件传输的,如果有实体传输那是实现不了的。

修改FeignConfig

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 FeignConfig {  
    @Autowired  
    private ObjectFactory<HttpMessageConverters> messageConverters;  
          
    @Bean  
    public Encoder feignFormEncoder() {  
        return new SpringFormEncoder(new SpringEncoder(messageConverters));  
    }  
 }  

Feign支持文件上传的:
https://www.jianshu.com/p/6f8d56992f33

相关文章

  • Feign 如何支持进行文件上传

    嗨咯大家好,好久没写了,疫情期间在家也变懒了,不想打开电脑,不想学习敲代码。不过今天在家上了一天班,遇到了一个问题...

  • Feign 如何支持进行文件上传

    嗨咯大家好,好久没写了,疫情期间在家也变懒了,不想打开电脑,不想学习敲代码。不过今天在家上了一天班,遇到了一个问题...

  • Feign上传文件

    今天给大家介绍下在Feign中如何调用文件上传接口,进行文件上传操作。 这边文章讲的Feign不是Spring C...

  • Feign 支持文件上传

    今天又尝试了一下Fegin做文件上传。这次使用的boot版本为2.2.2,cloud版本为Hoxton.SR1。然...

  • Spring Boot实现文件上传

    一、文件上传的配置 Spring Boot默认支持文件上传,对文件上传支持友好。对上传进行配置如下: Spring...

  • Spring Cloud Alibaba 实战(六) - 声明式

    本章学习Feign,包括Feign的自定义配置,实现文件上传,进一步实现代码的重用,Feign性能优化,Feign...

  • Feign文件上传问题

    单服务中使用MultipartFile上传文件没有问题,但Cloud中使用Feign文件上传就会有问题,不会自动跳...

  • Spring Cloud Feign如何上传文件

    去查看原文 一、Feign官方提供了feign 的子项目feign-from 1、加依赖 io.github.op...

  • 使用Spring Cloud Feign上传文件

    最近经常有人问Spring Cloud Feign如何上传文件。有团队的新成员,也有其他公司的兄弟。本文简单做个总...

  • Springcloud feign文件上传

    四、GET请求传递POJO 1、问题 Feign 默认不支持GET方法直接绑定POJO的(只支持基本类型),建议使...

网友评论

      本文标题:Feign 如何支持进行文件上传

      本文链接:https://www.haomeiwen.com/subject/miouyhtx.html