欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

feign调用上传文件接口(MultipartFile)

程序员文章站 2022-06-26 08:05:54
...

1.服务端MultipartFile这个类型的参数要用@RequestPart(“file”) 这个注解

@PostMapping("/uploadMultipleFile")
    public Result uploadImg(@RequestPart("file") MultipartFile[] files){
        try {
            return uploadService.uploadImg(files);
        }catch (Exception e){
            log.info("文件上传异常"+ ExceptionUtils.getStackTrace(e));
            return new Result(ResultCode.FAIL,"文件上传异常");
        }
    }

2.调用端加入如下坐标

 <!--feign-form-->
        <dependency>
            <groupId>io.github.openfeign.form</groupId>
            <artifactId>feign-form</artifactId>
            <version>3.8.0</version>
        </dependency>
        <!--配合spring使用-->
        <dependency>
            <groupId>io.github.openfeign.form</groupId>
            <artifactId>feign-form-spring</artifactId>
            <version>3.8.0</version>
        </dependency>

  <!--openfeign-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>

3.加入配置类

import feign.codec.Encoder;
import feign.form.spring.SpringFormEncoder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.context.annotation.Scope;


@Configuration
public class MultipartSupportConfig {

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

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

}

4.调用端feign接口

import com.qianxian.common.util.Result;
import com.qianxian.user.config.MultipartSupportConfig; 
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * 上传接口
 * @author yyj
 */
@FeignClient(name="ldd-upload",configuration =  MultipartSupportConfig.class)
public interface UploadControllerFeign {

    /**
     * 图片上传
     * @param files
     * @return
     */
    @RequestMapping(method = RequestMethod.POST, value = "/uploadMultipleFile",consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    Result uploadImg(@RequestPart("file") MultipartFile[] files);
}

5.controller层调用

 @PostMapping("/uploadMultipleFile")
    public Result uploadImg(@RequestPart("file") MultipartFile[] files){
        try {
            return uploadControllerFeign.uploadImg(files);
        }catch (Exception e){
            log.info("文件上传异常"+ ExceptionUtils.getStackTrace(e));
            return new Result(ResultCode.FAIL,"文件上传异常");
        }
    }
相关标签: feign