解决spring cloud consumer不能调用provider上传文件问题

一、第一种解决方案

<dependency>
<groupId>io.github.openfeign.form</groupId>
<artifactId>feign-form-spring</artifactId>
<version>3.2.2</version>
</dependency>
<dependency>
<groupId>io.github.openfeign.form</groupId>
<artifactId>feign-form</artifactId>
<version>3.2.2</version>
</dependency>
package com.***.mes.api.system;

/**
 * 文件服务
 * @Author ***
 * @Date 13:48 2019/4/8
 **/
@Service
@FeignClient(value = "pgxtmes-provider-system", configuration =  {FileFeignService.FeignMultipartSupportConfig.class} ,fallback = FileFeignServiceFallback.class)
public interface FileFeignService {

    String BASE_PATH = "/remote/file";

    /**
     * 上传文件至ftp
     * @Author ***
     * @Date 13:51 2019/4/8
     * @Param [file, fileDTO]
     * @return com.***.mes.bean.common.dto.ResultDTO
     **/
    @RequestMapping(value = BASE_PATH + "/uploadFtp", method = RequestMethod.POST,consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    ResultDTO uploadFtp(@RequestPart(value = "file") MultipartFile file,
                        @RequestParam(value = "createUserId") String createUserId,
                        @RequestParam(value = "createUsername")String createUsername);

    /**
     * 更新文件
     * @Author ***
     * @Date 13:56 2019/4/8
     * @Param [fileDTO]
     * @return com.xyzl.mes.bean.common.dto.ResultDTO
     **/
    @RequestMapping(value = BASE_PATH + "/updateFile", method = RequestMethod.POST)
    ResultDTO updateFile(@RequestBody FileDTO fileDTO);

    /**
     * 删除文件
     * @Author ***
     * @Date 9:15 2019/4/9
     * @Param [fileDTO]
     * @return com.***.mes.bean.common.dto.ResultDTO
     **/
    @RequestMapping(value = BASE_PATH + "/deleteFile", method = RequestMethod.POST)
    ResultDTO deleteFile(@RequestBody FileDTO fileDTO);


    @Configuration
    class FeignMultipartSupportConfig {

        @Autowired
        private ObjectFactory<HttpMessageConverters> messageConverters;

        @Bean
        @Primary
        @Scope("prototype")
        public Encoder feignEncoder() {
            return new SpringFormEncoder(new SpringEncoder(messageConverters));
        }
    }
}

二、第二种解决方案

    @Override
    public List<Long> uploadFile(UploadFileCommand uploadFileCommand) {
        MultipartFile[] files = uploadFileCommand.getFile();

        FileDataMapCommand command = new FileDataMapCommand();
        command.setModuleName(SystemEnum.MANAGEMENT.getCode());
        Map<String, byte[]> fileMap = new HashMap<>();
        for (MultipartFile file : files) {
            try {
                byte[] bytes = file.getBytes();
                String fileName = file.getOriginalFilename();
                fileMap.put(fileName, bytes);
            } catch (IOException e) {
                throw SaasBaseException.build(ResultEnum.FILE_UPLOAD_FAIL);
            }
        }
        command.setFileMap(fileMap);
        command.setMappingType(uploadFileCommand.getMappingType());
        ResultDTO upload = fileFeignService.uploadFile(command);
        return ResultDTOUtil.getDataList(upload, Long.class);
    }

原理是把文件转成二进制传入到某个具体的文件provider处理层

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值