Feign跨服务上传文件

1.feign multipart 跨服务传递文件配置类(非全局配置)

public class FeignMultipartConfig {

    @Bean
    public Encoder multipartFormEncoder() {
        return new SpringFormEncoder(new SpringEncoder(new ObjectFactory<HttpMessageConverters>() {
            @Override
            public HttpMessageConverters getObject() throws BeansException {
                return new HttpMessageConverters(new RestTemplate().getMessageConverters());
            }
        }));
    }

}

2.文件服务feign client

@FeignClient(name = "msfileupload", decode404 = true,configuration = FeignMultipartConfig.class)
public interface MsFileUploadFeignApi {

    @RequestMapping(value = "/upload/single", method = RequestMethod.POST,  produces = {MediaType.APPLICATION_JSON_UTF8_VALUE}
            ,consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    UploadResponse singleUpload(@RequestParam("bizType") String bizType, @RequestPart("file") MultipartFile file);

    @RequestMapping(value = "/upload/multi", method = RequestMethod.POST,produces = {MediaType.APPLICATION_JSON_UTF8_VALUE}
            ,consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    BaseRestResponse multiUpload(@RequestParam("bizType") String bizType, @RequestPart("files") MultipartFile[] files);

}

3.当前服务暴露给外部的feign client

@FeignClient(name = "demoservice", url = "${demoservice.feign.url:}" , fallback = FileUploadCommonApiFallBack.class)
public interface FileUploadCommonApi {

    @RequestMapping(value = "/common/file/upload/single", method = RequestMethod.POST)
    UploadResponse singleUpload(@RequestParam("bizType") String bizType, @RequestParam("file") MultipartFile file);

    @RequestMapping(value = "/common/file/upload/multi", method = RequestMethod.POST)
    BaseRestResponse multiUpload(@RequestParam("bizType") String bizType
            , @RequestParam("files") MultipartFile[] files);

}

4.当前服务控制器代码片段:

@Slf4j
@RestController("fileUploadCommonControllerV1")
@Api(value = "文件通用处理", tags= "文件上传通用控制器")
public class FileUploadCommonController implements FileUploadCommonApi {

    @Autowired
    MsFileUploadFeignApi msFileUploadFeignApi;

    @ApiOperation(value = "单个文件上传", notes = "单文件上传")
    @Override
    public UploadResponse singleUpload(@RequestParam("bizType") String bizType
            , @RequestParam("file") MultipartFile file) {
        log.info("FileUploadCommonController.singleUpload ...");
        return msFileUploadFeignApi.singleUpload(bizType,file);
    }

    @ApiOperation(value = "多个文件上传", notes = "多个文件上传")
    @Override
    public BaseRestResponse multiUpload(@RequestParam("bizType") String bizType
            , @RequestParam("files") MultipartFile[] files) {
        log.info("FileUploadCommonController.multiUpload ...");
        return msFileUploadFeignApi.multiUpload(bizType,files);
    }

}

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值