Spring Cloud feign 多文件上传

先说一下我的应用场景,第三方调用的我的接口,上传若干个文件,我用MultiPartFile[] 数组接收,之后我调用其他服务的接口,吧文件发送过去,统一保存。目前存在的问题就是,当你使用feign传递MultipartFile对象时,接收方无法解析,所以,需要重写feign的encoder,让它支持MultipartFile类型以及MultipartFile[]数组类型

 

首先加上两个jar包:

         
            io.github.openfeign.form
            feign-form
            3.3.0
        
        
            io.github.openfeign.form
            feign-form-spring
            3.3.0
        

这里重写FormEncoder使得feign可以支持文件传输:


import feign.RequestTemplate;
import feign.codec.EncodeException;
import feign.codec.Encoder;
import feign.form.ContentType;
import feign.form.FormEncoder;
import feign.form.MultipartFormContentProcessor;
import feign.form.spring.SpringManyMultipartFilesWriter;
import feign.form.spring.SpringSingleMultipartFileWriter;
import org.springframework.web.multipart.MultipartFile;

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


/**
 * @version: 1.00.00
 * @description:
 * @copyright:
 * @company: 
 * @author: panfan
 * @date: 2018/8/13 17:27
 * @history:
 */
public class SpringMultipartEncoder extends FormEncoder {

    /**
     * Constructor with the default Feign's encoder as a delegate.
     */
    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);
    }


}

下面做配置,为Encoder注入messageConverters:

@Configuration
public class FeignMultipartSupportConfig {


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

    @Autowired  
    private ObjectFactory messageConverters;  

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


}

 

我的文件上传接口如下,接受到文件,调用fileSystemService传输到文件处理组件:

 @RequestMapping(value = "/file/upload")
    @ResponseBody
    public ResourceUploadResponse uploadPicture(@RequestParam("access_token") String accessToken, HttpServletRequest request,
                                @RequestParam("files") MultipartFile[] files, HttpServletResponse response,
                                @RequestParam("file_type") int fileType) {



/**  中间代码略 **/

 // 上传图片,fileSystemService 是fastdfs文件操作的统一微服务。
        ResourceUploadResponse resultFeign = fileSystemService.saveImg(files, fileType);
        if(resultFeign.getCode() == 1){
            return new ResourceUploadResponse(Constant.RESPONSE_STATE_SUCCESS, Constant.RESPONSE_CODE_SUCCESS, "上传照片成功", resultFeign.getUrls());
        }else{
            return new ResourceUploadResponse(Constant.RESPONSE_STATE_FAIL, Constant.RESPONSE_CODE_FAIL, "接口调用失败", null);
        }

}

我的feignclient:

@FeignClient(value = "file-system", fallbackFactory = FileSystemServiceHystrix.class, configuration = SpringMultipartEncoder.class
(这里是SpringMultipartEncoder.class还是FeignMultipartSupportConfig.class需要你们试一试,评论说是后者,我的博客一开始写的是前者,两个都试试吧,之前的代码已不见了))
public interface FileSystemService {


    // 存储图片,返回所有图片在fastdfs上的urls
    @RequestMapping(method = RequestMethod.POST, value = "/rest/fs/resource/common/multiFileUpload",
            consumes = MediaType.MULTIPART_FORM_DATA_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
    ResourceUploadResponse saveImg(@RequestPart(value = "files") MultipartFile[] files, @RequestParam(value = "bizType") int bizType);


}

上面有个SpringMultipartEncoder,不要慌这个是我们重写刚才导入jar包中的FormEncoder(代码在下面),以让他支持任意数量文件上传,同时,这里用@RequestPart ,注意不能用@RequestParam,如果用@RequestParam,被调用的服务方会出现如下错误:

Caused by: org.apache.commons.fileupload.FileUploadException: the request was rejected because no multipart boundary was found

关于@RequestPart和@RequestParam的区别,我们看@RequestPart的注释,

Spring Cloud feign 多文件上传_第1张图片

好,这里说了,

他们最大的不同是,当请求方法的请求参数类型不再是String类型的时候,@RequestParam依赖于注册Converter或者PropertyEditor来解析参数,而 @RequestPart则是通过HttpMessageConverter来根据Content-Type决定采用的消息转换器。
总之,@RequestParam适用于name-valueString类型的请求域,@RequestPart适用于复杂的请求域(像JSON,XML)。


最后,我们的文件操作统一组件接口代码,也就是服务被调用方接受multipartfile的Controller方法,如下处理:

@PostMapping("/common/multiFileUpload")
    public ResourceUploadResponse multiFileUpload(@RequestParam("files") MultipartFile[] files, int bizType){
        
        // 略略略~~~
        return "上传成功";
    }

完美!!!

 

你可能感兴趣的:(SpringCloud,feign)