Solo  当前访客:3 登录 注册

喧哗博客-http://blog.xuahua.com

繁华过后的沉寂--技术经验分享
浏览次数: 95,037    文章总数: 91    评论总数: 3
标签:

springcloud feign 多文件上传 有更新!

网上也有不同的上传,但是对于多文件上传的解决方案比较少,参考了https://blog.csdn.net/qq_32953079/article/details/8163081

1,导入引用 的文件 

   

<dependency>
            <groupId>io.github.openfeign.form</groupId>
            <artifactId>feign-form</artifactId>
        </dependency>
        <dependency>
            <groupId>io.github.openfeign.form</groupId>
            <artifactId>feign-form-spring</artifactId>
        </dependency>
        <dependency>
            <groupId>commons-fileupload</groupId>
            <artifactId>commons-fileupload</artifactId>
        </dependency>

 

2,定义上传接口

 

@FeignClient(value = "PLACESERVICEPROVIDER", configuration = FeignMultipartSupportConfig.class)
public interface FileService {

 @PostMapping(value = "/file/uploadMulti", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public BaseResult uploadMulti(@RequestPart(value = "files") MultipartFile[] files) throws ServiceException;
}

@Configuration
public class FeignMultipartSupportConfig {
    @Autowired
    private ObjectFactory<HttpMessageConverters> messageConverters;

    @Bean
    public Encoder feignEncoder() {
        return new SpringMultipartEncoder(new SpringEncoder(messageConverters));
    }
}

public class SpringMultipartEncoder extends FormEncoder {

    public SpringMultipartEncoder() {
        this(new Default());
    }


    /**
     * Constructor with specified delegate encoder.
     *
     * @param delegate delegate encoder, if this encoder couldn't encode object.
     */
    public SpringMultipartEncoder(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 {
        // 单MultipartFile判断
        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数组处理
            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,服务端实现接口

  

@RestController
public class FileServiceImpl implements FileService {
   @Override
    public BaseResult uploadMulti(MultipartFile[] files) throws ServiceException{
  //do Something
}

}

 

 

 一般默认的上传文件大小,会限制1m大小,可以增加配置

具体设置参见 这篇文章的设置

 

 

 

 

 

公告

喧哗博客--繁华过后的沉寂--技术经验分享^-^
Copyright (c) 2009-2019, b3log.org & hacpai.com