Spring Cloud Feign文件传输的示例代码

时间:2021-11-05 11:43:37

一、配置文件解析器

服务提供者和消费者都需要配置文件解析器,这里使用 commons-fileupload 替换原有的解析器:

依赖:

?
1
2
3
4
5
<dependency>
  <groupid>commons-fileupload</groupid>
  <artifactid>commons-fileupload</artifactid>
  <version>1.3.1</version>
</dependency>

注入 bean :

?
1
2
3
4
5
6
@bean(name = "multipartresolver")
public multipartresolver mutipartresolver(){
  commonsmultipartresolver com = new commonsmultipartresolver();
  com.setdefaultencoding("utf-8");
  return com;
}

程序入口中剔除原有的解析器:

?
1
@springbootapplication(exclude = {multipartautoconfiguration.class})

二、服务提供者,即接收文件一方的配置

controller 的写法:

?
1
2
3
4
5
6
7
8
9
10
11
@responsebody
@requestmapping(value = "/upload", method = {requestmethod.post},
        produces = {mediatype.application_json_utf8_value},
        consumes = mediatype.multipart_form_data_value)
public result<string> uploadfile(@requestpart("file")multipartfile file,
@requestparam("id")long id){
  string filename = file.getoriginalfilename();
  string extend = fileoperateutil.suffix(filename);
  fileoperateutil.copy("e:\\" + filename, file);
  return resultbuilder.success("ok");
}

@requestpart 指定文件,后面的 @requestparam 是额外参数,注意额外参数不能超过url长度限制。

三、服务消费者配置

依赖:

?
1
2
3
4
5
6
7
8
9
10
<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>

文件编码配置:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import feign.codec.encoder;
import feign.form.spring.springformencoder;
import org.springframework.beans.factory.objectfactory;
import org.springframework.beans.factory.annotation.autowired;
import org.springframework.boot.autoconfigure.web.httpmessageconverters;
import org.springframework.cloud.netflix.feign.support.springencoder;
import org.springframework.context.annotation.bean;
import org.springframework.context.annotation.configuration;
 
@configuration
public class multipartsupportconfig{
 
  @autowired
  private objectfactory<httpmessageconverters> messageconverters;
 
  @bean
  public encoder feignformencoder(){
    return new springformencoder(new springencoder(messageconverters));
  }
 
}

feign 接口定义:

?
1
2
3
4
5
6
7
8
9
10
11
@feignclient(name = "test-upload")
public interface uploadservice{
 
  @responsebody
  @requestmapping(value = "/upload", method = {requestmethod.post},
      produces = {mediatype.application_json_utf8_value},
      consumes = mediatype.multipart_form_data_value)
  result<string>uploadfile(@requestpart("file")multipartfile file,
@requestparam("id")long id);
 
}

与普通 feign 接口写法差不多,注意方法注解和参数与服务提供者的 controller 一样。

controller 的写法, controller 中接收前端传过来的文件信息和额外参数,然后通过 feign 接口传输到远端:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// 注入 feign 接口
@autowired
private uploadservice uploadservice;
 
@requestmapping(value = "/upload", method = requestmethod.post, produces = "application/json; charset=utf-8")
@responsebody
public result<string> testupload(httpservletrequest request, long id){
  result<string> result = null;
  multiparthttpservletrequest mrequest = (multiparthttpservletrequest) request;
  map<string, multipartfile> filemap = mrequest.getfilemap();
  for (multipartfile mfile : filemap.values()) {
    string filename = mfile.getoriginalfilename();
    result = uploadservice.uploadfile(mfile, id);
  }
  return result;
}

四、总结

最后梳理一下流程,服务消费者接收前端(如浏览器)传过来的文件,但是并不进行业务处理,然后通过 feign 调用接口,把文件传给服务提供者,服务提供者拿到文件后,进行相应的业务处理。

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

原文链接:http://www.ciphermagic.cn/spring-cloud-feign-upload.html