spring cloud feign实现远程调用服务传输文件的方法
实践案例包括两个项目,服务提供者项目名:upload-service,调用服务项目名:upload-client,主要给出两个服务之间的调用过程,文件上传功能不提供
项目框架:spring-boot2.0.1.RELEASE、spring-cloudFinchley.RELEASE
依赖:
io.github.openfeign.form feign-form 3.0.3 io.github.openfeign.form feign-form-spring 3.0.3 commons-fileupload commons-fileupload 1.3.3
创建FeignClient接口(用于指定远程调用的服务)
//申明这是一个Feign客户端,并且指明服务id
@FeignClient(value="com-spring-caclulate")
publicinterfaceCacluFeignClient{
//这里定义了类似于SpringMVC用法的方法,就可以进行RESTful的调用了
@RequestMapping(value="/caclu/{num}",method=RequestMethod.GET)
publicItemcaclulate(@PathVariable("num")Integernum);
}
一.文件上传服务upload-service
1.控制层
@Slf4j
@CrossOrigin
@RestController
@RequestMapping("/ftp")
@Api(description="文件上传控制")
publicclassFtpFileController{
@Autowired
privateFtpFileServiceftpFileService;
/**
*FTP文件上传
*
*@return
*/
@PostMapping(value="/uploadFile",consumes=MediaType.MULTIPART_FORM_DATA_VALUE)
publicFtpApiResponseuploadFileFTP(@RequestPart(value="file")MultipartFilefile,
@RequestParam("logId")StringlogId){
FtpApiResponseresult=newFtpApiResponse<>();
LogUtil.updateLogId(logId);
try{
log.info("文件上传开始!}");
LongstartTime=System.currentTimeMillis();
FtpUploadResDTOresDTO=ftpFileService.uploadFile(file);
result.setData(resDTO);
result.setSuccess(true);
result.setTimeInMillis(System.currentTimeMillis()-startTime);
log.info("文件上传结束resDTO:{},耗时:{}",resDTO,(System.currentTimeMillis()-startTime));
}catch(ServiceExceptione){
result.setSuccess(false);
result.setErrorCode(ErrorMsgEnum.FILE_UPLOAD_EXCEPTION.getCode());
result.setErrorMsg(ErrorMsgEnum.FILE_UPLOAD_EXCEPTION.getMsg());
}catch(Exceptione){
result.setSuccess(false);
result.setErrorCode(ErrorMsgEnum.SYSTEM_ERROR.getCode());
result.setErrorMsg(ErrorMsgEnum.SYSTEM_ERROR.getMsg());
log.info("文件上传失败Exception:{}",Throwables.getStackTraceAsString(e));
}
returnresult;
}
}
2.业务层
@Service
@Slf4j
publicclassFtpFileService{
@Autowired
privateFtpFileManagerftpFileManager;
/**
*上传文件
*
*@paramfile
*@return
*/
publicFtpUploadResDTOuploadFile(MultipartFilefile){
try{
//判断上传文件是否为空
if(null==file||file.isEmpty()||file.getSize()==0){
log.info("传入的文件为空,file:{}",file);
thrownewServiceException(ErrorMsgEnum.EMPTY_FILE);
}
//文件上传至ftp服务目录
FtpFileRecordDOftpFileRecordDO=ftpFileManager.fileUploadToFtp(file);
if(null==ftpFileRecordDO){
log.info("文件上传至ftp服务目录异常");
thrownewServiceException(ErrorMsgEnum.FILE_UPLOAD_TO_FTP_EXCEPTION);
}
returnftpFileManager.addFileRecord(ftpFileRecordDO);
}catch(Exceptione){
log.error("业务异常,case",e);
thrownewServiceException(ErrorMsgEnum.SYSTEM_ERROR);
}
}
}
3.服务写好后,需要把远程接口暴露出去
@FeignClient(value="upload-service",configuration=UpDownFtpFacade.MultipartSupportConfig.class)
publicinterfaceUpDownFtpFacade{
/**
*FTP上传文件
*
*@paramfile文件
*@paramlogId日志id
*@return
*/
@PostMapping(value="/ftp/uploadFile",consumes=MediaType.MULTIPART_FORM_DATA_VALUE)
FtpApiResponseuploadFileFTP(@RequestPart(value="file")MultipartFilefile,
@RequestParam("logId")StringlogId);
/**
*引用配置类MultipartSupportConfig.并且实例化
*/
@Configuration
classMultipartSupportConfig{
@Bean
publicEncoderfeignFormEncoder(){
returnnewSpringFormEncoder();
}
}
}
二.文件上传客户端upload-client
@Slf4j
@Component
publicclassFileManager{
@Autowired
privateUpDownFtpFacadeupDownFtpFacade;
/**
*调用远程上传文件接口
*
*@paramfile待上传的文件
*@return下载路径
**/
publicFtpApiResponserequestFtpFacade(MultipartFilefile){
try{
DiskFileItemfileItem=(DiskFileItem)newDiskFileItemFactory().createItem("file",
MediaType.ALL_VALUE,true,file.getOriginalFilename());
InputStreaminput=file.getInputStream();
OutputStreamos=fileItem.getOutputStream();
IOUtils.copy(input,os);
MultipartFilemulti=newCommonsMultipartFile(fileItem);
FtpApiResponseresponse=upDownFtpFacade.uploadFileFTP(multi,LogUtil.getLogId());
if(null==response||!response.getSuccess()||null==response.getData()){
thrownewManagerException(ErrorMsgEnum.FIlE_UPLOAD_FAILED);
}
returnresponse;
}catch(Exceptione){
thrownewManagerException(ErrorMsgEnum.FIlE_UPLOAD_FAILED);
}
}
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。