Springboot Feign文件上传

发布时间:2020-04-09 15:35:30 作者:咖fei丶
来源:网络 阅读:279

服务提供者

@SpringBootApplication
public class FeignUploadFirstApplication {
  @RestController
  public class UploadController {

    @RequestMapping(value = "/uploadFile",method = RequestMethod.POST, consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public String handleFileUpload(@RequestPart(value = "file") MultipartFile file) {
      return file.getOriginalFilename();
    }
  }
  public static void main(String[] args) {
    SpringApplication.run(FeignUploadFirstApplication.class, args);
  }
}

服务消费者

1.feign相关的依赖

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

2.feign配置类

@Configuration
public class FeignMultipartSupportConfig {
    @Bean
    public Encoder feignFormEncoder() {
        return new SpringFormEncoder();
    }
}

3.feign调用接口 注解中加入配置类

@FeignClient(value = "xxx", configuration = FeignMultipartSupportConfig.class)
public interface IUploadService {
    /**
     * 文件上传
     * @param file 文件
     * @return
     */
    @RequestMapping(value = "upload/uppic", method = RequestMethod.POST, consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    ResultModel<String> uppic(@RequestPart(value = "file")MultipartFile file, @RequestParam Map<String,Object> map);

}

4.上传接口

@RestController
public class UploadController {
  @Autowired
   IUploadService uploadService;

  @RequestMapping(value = "/uploadFile",method = RequestMethod.POST, consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
  public String handleFileUpload(@RequestPart(value = "file") MultipartFile file) {
    return uploadService.uppic(file);
  }
}

文件上传 File 转 Multi

FileInputStream fileInputStream = new FileInputStream(file);
MultipartFile multipartFile = new MockMultipartFile("file", file.getName(), ContentType.TEXT_PLAIN.toString(), fileInputStream);
推荐阅读:
  1. springboot中FeignClient注解如何使用
  2. SpringBoot如何使用feign实现远程接口调用和错误熔断

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

springboot文件上传 feign文件上传 bo

上一篇:关于CA证书的申请流程介绍

下一篇:DVSSL证书如何申请?DVSSL证书价格是多少?

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》