欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

spring cloud feign实现远程调用服务传输文件的方法

程序员文章站 2024-02-26 19:44:28
实践案例包括两个项目,服务提供者项目名:upload-service,调用服务项目名:upload-client,主要给出两个服务之间的调用过程,文件上传功能不提供 项目...

实践案例包括两个项目,服务提供者项目名:upload-service,调用服务项目名:upload-client,主要给出两个服务之间的调用过程,文件上传功能不提供

项目框架:spring-boot 2.0.1.release、spring-cloud finchley.release

依赖:

  <dependency>
   <groupid>io.github.openfeign.form</groupid>
   <artifactid>feign-form</artifactid>
   <version>3.0.3</version>
  </dependency>
  <dependency>
   <groupid>io.github.openfeign.form</groupid>
   <artifactid>feign-form-spring</artifactid>
   <version>3.0.3</version>
  </dependency>
  <dependency>
   <groupid>commons-fileupload</groupid>
   <artifactid>commons-fileupload</artifactid>
   <version>1.3.3</version>
  </dependency>

// 申明这是一个feign客户端,并且指明服务id
@feignclient(value = "com-spring-caclulate") 
public interface caclufeignclient {

 // 这里定义了类似于springmvc用法的方法,就可以进行restful的调用了
 @requestmapping(value = "/caclu/{num}", method = requestmethod.get)
 public item caclulate(@pathvariable("num") integer num);

} 

一.文件上传服务upload-service

1.控制层

@slf4j
@crossorigin
@restcontroller
@requestmapping("/ftp")
@api(description = "文件上传控制")
public class ftpfilecontroller {

 @autowired
 private ftpfileservice ftpfileservice;

 /**
  * ftp文件上传
  *
  * @return
  */
 @postmapping(value = "/uploadfile", consumes = mediatype.multipart_form_data_value)
 public ftpapiresponse<ftpuploadresdto> uploadfileftp(@requestpart(value = "file") multipartfile file,
               @requestparam("logid") string logid) {
  ftpapiresponse<ftpuploadresdto> result = new ftpapiresponse<>();
  logutil.updatelogid(logid);
  try {
   log.info("文件上传开始!}");
   long starttime = system.currenttimemillis();
   ftpuploadresdto resdto = ftpfileservice.uploadfile(file);
   result.setdata(resdto);
   result.setsuccess(true);
   result.settimeinmillis(system.currenttimemillis() - starttime);
   log.info("文件上传结束 resdto:{},耗时:{}", resdto, (system.currenttimemillis() - starttime));
  } catch (serviceexception e){
   result.setsuccess(false);
   result.seterrorcode(errormsgenum.file_upload_exception.getcode());
   result.seterrormsg(errormsgenum.file_upload_exception.getmsg());
  } catch (exception e) {
   result.setsuccess(false);
   result.seterrorcode(errormsgenum.system_error.getcode());
   result.seterrormsg(errormsgenum.system_error.getmsg());
   log.info("文件上传失败 exception:{}", throwables.getstacktraceasstring(e));
  }
  return result;
 }
}

2.业务层

@service
@slf4j
public class ftpfileservice {

 @autowired
 private ftpfilemanager ftpfilemanager;

 /**
  * 上传文件
  *
  * @param file
  * @return
  */
 public ftpuploadresdto uploadfile(multipartfile file) {
  try {
   //判断上传文件是否为空
   if (null == file || file.isempty() || file.getsize() == 0) {
    log.info("传入的文件为空,file:{}", file);
    throw new serviceexception(errormsgenum.empty_file);
   }
   //文件上传至ftp服务目录
   ftpfilerecorddo ftpfilerecorddo = ftpfilemanager.fileuploadtoftp(file);
   if (null == ftpfilerecorddo) {
    log.info("文件上传至ftp服务目录异常");
    throw new serviceexception(errormsgenum.file_upload_to_ftp_exception);
   }
   return ftpfilemanager.addfilerecord(ftpfilerecorddo);
  } catch (exception e) {
   log.error("业务异常,case", e);
   throw new serviceexception(errormsgenum.system_error);
  }
 }
}

3.服务写好后,需要把远程接口暴露出去

@feignclient(value = "upload-service", configuration = updownftpfacade.multipartsupportconfig.class)
public interface updownftpfacade {

 /**
  * ftp上传文件
  *
  * @param file 文件
  * @param logid 日志id
  * @return
  */
 @postmapping(value = "/ftp/uploadfile",consumes = mediatype.multipart_form_data_value)
 ftpapiresponse<ftpuploadresdto> uploadfileftp(@requestpart(value = "file") multipartfile file,
             @requestparam("logid") string logid);

 /**
  * 引用配置类multipartsupportconfig.并且实例化
  */
 @configuration
 class multipartsupportconfig {
  @bean
  public encoder feignformencoder() {
   return new springformencoder();
  }
 }

}

二.文件上传客户端upload-client

@slf4j
@component
public class filemanager {
  @autowired
  private updownftpfacade updownftpfacade;
  
  /**
  * 调用远程上传文件接口
  *
  * @param file 待上传的文件
  * @return 下载路径
  **/
 public ftpapiresponse<ftpuploadresdto> requestftpfacade(multipartfile file) {
  try {
   diskfileitem fileitem = (diskfileitem) new diskfileitemfactory().createitem("file",
     mediatype.all_value, true, file.getoriginalfilename());
   inputstream input = file.getinputstream();
   outputstream os = fileitem.getoutputstream();
   ioutils.copy(input, os);
   multipartfile multi = new commonsmultipartfile(fileitem);
   ftpapiresponse<ftpuploadresdto> response = updownftpfacade.uploadfileftp(multi, logutil.getlogid());
   if (null == response || !response.getsuccess() || null == response.getdata()) {
    throw new managerexception(errormsgenum.file_upload_failed);
   }
   return response;
  } catch (exception e) {
   throw new managerexception(errormsgenum.file_upload_failed);
  }
  }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。