zoukankan      html  css  js  c++  java
  • SpringCloud---Feign上传下载详解

    1.使用原因

     公司最近做的项目在用SpringCloud,涉及到了上传。但是Feign本身是不支持文件类型的。所以这里把上传下载的实现分享一下。

    2.所需配置

      这是自己实现的一个formEncoder,可以支持单文件和数组的多文件上传

    public class FeignSpringFormEncoder extends FormEncoder {
     
        /**
         * Constructor with the default Feign's encoder as a delegate.
         */
        public FeignSpringFormEncoder() {
            this(new Default());
        }
    
     
        /**
         * Constructor with specified delegate encoder.
         *
         * @param delegate delegate encoder, if this encoder couldn't encode object.
         */
        public FeignSpringFormEncoder(Encoder delegate) {
            super(delegate);
    
            MultipartFormContentProcessor processor = (MultipartFormContentProcessor) getContentProcessor(ContentType.MULTIPART);
            processor.addWriter(new SpringSingleMultipartFileWriter());
            processor.addWriter(new SpringManyMultipartFilesWriter());
        }
     
     
        @Override
        public void encode(Object object, Type bodyType, RequestTemplate template) throws EncodeException {
            if (bodyType.equals(MultipartFile.class)) {
                MultipartFile file = (MultipartFile) object;
                Map data = Collections.singletonMap(file.getName(), object);
                super.encode(data, MAP_STRING_WILDCARD, template);
                return;
            } else if (bodyType.equals(MultipartFile[].class)) {
                MultipartFile[] file = (MultipartFile[]) object;
                if(file != null) {
                    Map data = Collections.singletonMap(file.length == 0 ? "" : file[0].getName(), object);
                    super.encode(data, MAP_STRING_WILDCARD, template);
                    return;
                }
            }
            super.encode(object, bodyType, template);
        }
    }

    将实现的类注册一下。
    @Bean
    public Encoder feignEncoder(ObjectFactory<HttpMessageConverters> messageConverters) {
        return new FeignSpringFormEncoder(new SpringEncoder(messageConverters));
    }


    调用方的代码,这里参数接收的时候用的是@RequestPart,与@RequestParam区别大家可以去查一下。
    @PostMapping(value = "/upload", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    @ResponseBody
    public ApiResult upload(@RequestPart(value = "file") MultipartFile file) {
        return fileUploadApiClient.upload(file);
    }


    暴露的fileUploadApiClient接口还需要添加依赖
    <dependency>
        <groupId>io.github.openfeign.form</groupId>
        <artifactId>feign-form</artifactId>
        <version>3.3.0</version>
    </dependency>
    <dependency>
        <groupId>io.github.openfeign.form</groupId>
        <artifactId>feign-form-spring</artifactId>
        <version>3.3.0</version>
    </dependency>

       暴露的fileUploadApiClient代码,MediaType类型的指定

    @PostMapping(value = "/oss/upload", consumes = MediaType.MULTIPART_FORM_DATA_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
    ApiResult upload(@RequestPart(value = "file") MultipartFile file);


    最后直接调用就可以上传成功.
  • 相关阅读:
    教你用photoshop cs5或者cs6做IPad,背景随意换,gif制作,高清教程,原创
    ASP.NET MVC4 IN ACTION学习笔记第一波
    潜移默化学会C#不常用语法《1》动态类型绑定dynamic
    SubSnoic 框架入门到提高(1)全程记录
    杨洋疯狂C# 刊号:201208 第1期ASPNET验证(一)
    杨洋疯狂C# 刊号:201207 第1期
    ASP.NET MVC4 IN ACTION学习笔记第二波
    JavaScript深入【表达式和运算符(上集)】你能过我8关js运算符的题目吗?
    清新空气我的.net(C#)生涯知识总结 跨CSS,JS,JAVA,AJAX,WPF,WCF,LINQ,ASP.NET,Winform,Sqlserver,Mysql,EF,OOP,开发工具等
    潜移默化学会WPF(Treeview异步加载节点)
  • 原文地址:https://www.cnblogs.com/technologykai/p/9335029.html
Copyright © 2011-2022 走看看