java微服务有导出_使用Feign实现微服务间文件传输

在很多时候我们会遇到微服务之间文件传输,很多时候我们可以通过序列化等方式解决(如图片等)。

最近项目中有个excel上传,以及多媒体文件上传,直接报错。

也试了2种解决方式,都不可行。

1.写一个文件Encoder解析器,会出现其他的rest请求出现encoder错误

2.springcloud feign有一个规范,不可以传输2个对象,可以是一个对象带几个参数方式。

那么我们现在需要一种方式,不配置全局的解析器,而是通过Feign Builder 去管理上传文件,这种方式管理起来也较为方便。

引用包

com.netflix.feign

feign-core

8.17.0

com.netflix.feign

feign-jackson

8.17.0

com.netflix.feign

feign-slf4j

8.17.0

调用方式

@ApiOperation(value = "上传Excel", notes = "上传Excel")

@RequestMapping(value = "/imExcel", method = RequestMethod.POST, produces = request_headers)

public ActionResult imExcel(@RequestBody MultipartFile file,@RequestParam("operatorId") Integer operatorId){

if(file == null || file.isEmpty()|| operatorId==null)

return new ActionResult<>(ResultType.BAD_REQUEST,"文件与操作用户ID都不能为空");

String fileName = file.getOriginalFilename();

if (!fileName.matches("^.+\\.(?i)(xls)$") && !fileName.matches("^.+\\.(?i)(xlsx)$")) {

return new ActionResult<>(ResultType.BAD_REQUEST,"上传文件格式错误,请上传后缀为.xls或.xlsx的文件");

}

Feign.Builder encoder = Feign.builder()

.decoder(new JacksonDecoder())

.encoder(new FeignEncoder());

FileUpload complainFileUpload = encoder.target(FileUpload.class,LABEL_URL);

return complainFileUpload.imComplainExcel(file,operatorId);

}

文件Encode

import feign.RequestTemplate;

import feign.codec.EncodeException;

import feign.codec.Encoder;

import org.springframework.core.io.InputStreamResource;

import org.springframework.core.io.Resource;

import org.springframework.http.HttpEntity;

import org.springframework.http.HttpHeaders;

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值