springcloud feign 多文件上传

1.添加maven引用

<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>
        <dependency>
            <groupId>commons-fileupload</groupId>
            <artifactId>commons-fileupload</artifactId>
            <version>1.3.3</version>
        </dependency>

2.编写 SpringFormEncoder 处理多个文件上传

package com.bsx.config;

import feign.RequestTemplate;
import feign.codec.EncodeException;
import feign.codec.Encoder;
import feign.form.ContentProcessor;
import feign.form.MultipartFormContentProcessor;
import feign.form.spring.SpringFormEncoder;
import feign.form.spring.SpringManyMultipartFilesWriter;
import feign.form.spring.SpringSingleMultipartFileWriter;
import lombok.val;
import org.springframework.web.multipart.MultipartFile;

import java.lang.reflect.Type;
import java.util.Collections;
import java.util.Map;

import static feign.form.ContentType.MULTIPART;

/**
 * @Description: 处理多个文件上传
 * @author: ztd
 * @date 2019/6/14 下午4:26
 */
public class FeignSpringFormEncoder extends SpringFormEncoder {
    public FeignSpringFormEncoder(Encoder delegate) {
        super(delegate);
        MultipartFormContentProcessor processor = (MultipartFormContentProcessor) getContentProcessor(MULTIPART);
        processor.addWriter(new SpringSingleMultipartFileWriter());
        processor.addWriter(new SpringManyMultipartFilesWriter());
    }

    @Override
    public void encode(Object object, Type bodyType, RequestTemplate template) throws EncodeException {
        if (bodyType != null && 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);
    }
}

3.Client端配置这个 Encoder Configuration

package com.bsx.client;

import com.bsx.config.FeignSpringFormEncoder;
import feign.codec.Encoder;
import org.springframework.beans.factory.ObjectFactory;
import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.cloud.openfeign.support.SpringEncoder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.multipart.MultipartFile;

/**
 * @Description:
 * @author: ztd
 * @date 2019/6/12 下午7:31
 */
@FeignClient(name= "service-hi", configuration = UploadClient.FeignSupportConfig.class)
public interface UploadClient {
    /**
     * 单个文件上传
     * @param type
     * @param file
     * @return
     */
    @PostMapping(value ="/api/upload",produces = {MediaType.APPLICATION_JSON_UTF8_VALUE}, consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    String upload(@RequestParam(value = "type", required = false) String type,
                           @RequestPart(value="file", required = false)MultipartFile file);

    /**
     * 多个文件上传
     * @param type
     * @param files
     * @return
     */
    @PostMapping(value ="/api/multiUpload",produces = {MediaType.APPLICATION_JSON_UTF8_VALUE}, consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    String multiUpload(@RequestParam(value = "type", required = false) String type,
                                @RequestPart(value="files", required = false)MultipartFile[] files);

    @Configuration
    class FeignSupportConfig {
        @Bean
        public Encoder encoder(ObjectFactory<HttpMessageConverters> messageConverters) {
            return new FeignSpringFormEncoder(new SpringEncoder(messageConverters)) ;
        }
    }
}

4.实际的controller处理

package com.bsx.controller;

import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * @Description:
 * @author: ztd
 * @date 2019/6/17 下午2:56
 */
@RequestMapping("/api")
@RestController
public class UploadController {
    /**
     * 单个文件上传
     * @param type
     * @param file
     * @return
     */
    @PostMapping(value ="/upload",produces = {MediaType.APPLICATION_JSON_UTF8_VALUE}, consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public String upload(@RequestParam(value = "type", required = false) String type,
                           @RequestPart(value="file", required = false)MultipartFile file) {
        System.out.println(file.getOriginalFilename());
        return "成功!";
    }

    /**
     * 多个文件上传
     * @param type
     * @param files
     * @return
     */
    @PostMapping(value ="/multiUpload",produces = {MediaType.APPLICATION_JSON_UTF8_VALUE}, consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public String multiUpload(@RequestParam(value = "type", required = false) String type,
                                @RequestPart(value="files", required = false)MultipartFile[] files) {
        for (MultipartFile file : files) {
            System.out.println(file.getOriginalFilename());
        }
        return "成功!";
    }
}

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值