• 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开发工具下载地址及安装教程大全,点这里

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

  • 相关阅读:
    文件的权限
    正则表达式
    软硬链接的学习
    linux系统中的文件类型和扩展名
    把数组排成最小的数
    整数中1出现的次数(从1到n整数中1出现的次数)
    最小的K个数
    连续子数组的最大和
    数组中出现次数超过一半的数字
    字符串的排列
  • 原文地址:https://www.cnblogs.com/darendu/p/12706383.html
Copyright © 2020-2023  润新知