服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|JavaScript|易语言|

服务器之家 - 编程语言 - Java教程 - spring cloud feign实现远程调用服务传输文件的方法

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

2021-05-29 12:37蓝之鱼 Java教程

这篇文章主要介绍了spring cloud feign实现远程调用服务传输文件的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

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

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

依赖:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<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>

?
1
2
3
4
5
6
7
8
9
// 申明这是一个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.控制层

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
@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.业务层

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
@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.服务写好后,需要把远程接口暴露出去

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
@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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
@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);
  }
  }
}

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

原文链接:https://segmentfault.com/a/1190000016234692

延伸 · 阅读

精彩推荐