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 文件下载
    Asp.net 加密解密类
    ASP.Net 获取服务器信息
    Visual Studio 2013 和 ASP.NET 预览
    Windows Server 2012安装时所需要的KEY
    WordPress主题模板层次和常用模板函数
    小meta的大作用
    《淘宝技术这十年》之LAMP架构的网站
    面试题(八)
    面试题(七)
  • 原文地址:https://www.cnblogs.com/darendu/p/12706383.html
Copyright © 2011-2022 走看看