Spring Cloud feign 多文件上传

spring cloud fein文件上传,单文件,多文件等,网上参考资料很少,基本都不能用,通过这篇博客http://www.cnblogs.com/technologykai/p/9335029.html  经试验可用!,但他博客不够详细,我这里贴出我所有哦的详细代码,嘿嘿~

先说一下我的应用场景,第三方调用的我的接口,上传若干个文件,我用MultiPartFile[] 数组接收,之后我调用其他服务的接口,吧文件发送过去,统一保存

我的接口如下:

 @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);
        }

}

加上两个jar包:

         <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>

我的feignclient:

@FeignClient(value = "file-system", fallbackFactory = FileSystemServiceHystrix.class, configuration = SpringMultipartEncoder.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的注释,

好,这里说了,

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

这里是SpringMultipartEncoder 的配置:


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<HttpMessageConverters> messageConverters) {
//        return new SpringMultipartEncoder(new SpringEncoder(messageConverters));
//    }

    @Autowired  
    private ObjectFactory<HttpMessageConverters> messageConverters;  

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


}

最后,我们的文件操作统一微服务代码,也就是服务被调用方,如下处理:

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

完美!!!!不能用你打我!!!啊哈哈哈哈打不到我吧,没有办法,我就是。。。

猜你喜欢

转载自blog.csdn.net/qq_32953079/article/details/81630817
今日推荐