zoukankan      html  css  js  c++  java
  • Spring Cloud Feign如何上传文件

    去查看原文

    一、Feign官方提供了feign 的子项目feign-from

    1、加依赖

    io.github.openfeign.form feign-form 3.0.3 io.github.openfeign.form feign-form-spring 3.0.3

    java开发工具下载地址及安装教程大全,点这里

    更多深度技术文章,在这里

    2、编写Feign Client

    @FeignClient(name = “ms-content-sample”, configuration = UploadFeignClient.MultipartSupportConfig.class)

    public interface UploadFeignClient {

    @RequestMapping(value = “/upload”, method = RequestMethod.POST,

    produces = {MediaType.APPLICATION_JSON_UTF8_VALUE},

    consumes = MediaType.MULTIPART_FORM_DATA_VALUE)

    @ResponseBody

    String handleFileUpload(@RequestPart(value = “file”) MultipartFile file);

    class MultipartSupportConfig {

    @Bean public Encoder feignFormEncoder() {

    return new SpringFormEncoder();

    }

    }

    }

    如代码所示,在这个Feign Client中,我们引用了配置类MultipartSupportConfig ,在MultipartSupportConfig 中,我们实例化了

    SpringFormEncoder 。这样这个Feign Client就能够上传了

    注意:

    @RequestMapping(value = “/upload”, method = RequestMethod.POST,

    produces = {MediaType.APPLICATION_JSON_UTF8_VALUE},

    consumes = MediaType.MULTIPART_FORM_DATA_VALUE)

    (1)、produeces 、consumes 不能少

    (2)、接口定义中的注解@RequestPart(value = “file”) 不能写成@RequestParam(value = “file”

    (3)、最好将Hystrix的超时时间设长一点,例如5秒,否则可能文件还没上传完,Hystrix就超时了,从而导致客户端侧的报错

    原创文章,转载请注明出处。

    java开发工具下载地址及安装教程大全,点这里

    更多深度技术文章,在这里

  • 相关阅读:
    微信公众号对接配置
    ASP.NET MVC5+EF6+EasyUI 后台管理系统(89)-国际化,本地化,多语言应用
    Nacos安装教程
    IDEA 中创建SpringBoot 父子模块
    解决死锁之路(终结篇)
    CentOS安装node和npm
    CentOS安装RabbitMQ
    在LibreOffice中插入代码
    PowerShell查找程序路径
    使用命令行调用控制面板的选项
  • 原文地址:https://www.cnblogs.com/darendu/p/12706383.html
Copyright © 2011-2022 走看看