Feign framework does not directly support file uploads (Multipart/form-data). Need to integrate the module feign-form to achieve .

Using Feign independently

Adding module dependencies.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
<!-- Feign框架核心 -->
<dependency>
    <groupId>io.github.openfeign</groupId>
    <artifactId>feign-core</artifactId>
    <version>11.1</version>
</dependency>
<!-- 支持表单格式,文件上传格式 -->
<dependency>
    <groupId>io.github.openfeign.form</groupId>
    <artifactId>feign-form</artifactId>
    <version>3.8.0</version>
</dependency>
<!-- 文件操作工具类 -->
<dependency>
    <groupId>commons-io</groupId>
    <artifactId>commons-io</artifactId>
    <version>2.11.0</version>
</dependency>

Upload files

Define the interface.

 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
42
43
44
45
46
47
48
49
50
51
52
53
public interface FileUploadAPI {
    // 上传文件:参数为单个文件对象
    @RequestLine("POST /test/upload/single")
    @Headers("Content-Type: multipart/form-data")
    String upload(@Param("file") File file);

    // 上传文件:参数文多个文件对象
    @RequestLine("POST /test/upload/batch")
    @Headers("Content-Type: multipart/form-data")
    String upload(@Param("files") File[] files);

    // 上传文件:参数文多个文件对象
    @RequestLine("POST /test/upload/batch")
    @Headers("Content-Type: multipart/form-data")
    String upload(@Param("files") List<File> files);

    // 上传文件:参数为文件字节数组(这种方式在服务端无法获取文件名,不要使用)
    @RequestLine("POST /test/upload/single")
    @Headers("Content-Type: multipart/form-data")
    String upload(@Param("file") byte[] bytes);

    // 上传文件:参数为FormData对象
    @RequestLine("POST /test/upload/single")
    @Headers("Content-Type: multipart/form-data")
    String upload(@Param("file") FormData photo);

    // 上传文件:参数为POJO对象
    @RequestLine("POST /test/upload/single")
    @Headers("Content-Type: multipart/form-data")
    String upload(@Param("file") MyFile myFile);

    class MyFile {
        @FormProperty("is_public")
        Boolean isPublic;
        File file;

        public Boolean getPublic() {
            return isPublic;
        }

        public void setPublic(Boolean aPublic) {
            isPublic = aPublic;
        }

        public File getFile() {
            return file;
        }

        public void setFile(File file) {
            this.file = file;
        }
    }
}

Calling interface.

 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
FileAPI fileAPI = Feign.builder()
        .encoder(new FormEncoder()) // 必须明确设置请求参数编码器
        .logger(new Slf4jLogger())
        .logLevel(Logger.Level.FULL)
        .target(FileAPI.class, "http://localhost:8080");
File file1 = new File("C:\\Users\\xxx\\Downloads\\test1.jpg");
File file2 = new File("C:\\Users\\xxx\\Downloads\\test2.jpg");

// 上传文件1:参数为文件对象
fileAPI.upload(file1);

// 上传文件2:参数为字节数组(注意:在服务端无法获取到文件名)
byte[] bytes = FileUtils.readFileToByteArray(file1);
fileAPI.upload(bytes);

// 上传文件3:参数为FormData对象
byte[] bytes = FileUtils.readFileToByteArray(file1);
FormData formData = new FormData("image/jpg", "test1.jpg", bytes);
String result = fileAPI.upload(formData);

// 上传文件4:参数为POJO对象
FileAPI.MyFile myFile = new FileAPI.MyFile();
myFile.setPublic(true);
myFile.setFile(file1);
fileAPI.upload(myFile);

// 上传文件:参数为多个文件
fileAPI.upload(new File[]{file1, file2});
fileAPI.upload(Arrays.asList(new File[]{file1, file2}));

Download the file

Define the interface.

1
2
3
4
5
public interface FileDownloadAPI {
    // 下载文件
    @RequestLine("GET /test/download/file")
    Response download(@QueryMap Map<String, Object> queryMap);
}

Calling interface.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
// 下载文件时返回值为Response对象,不需要设置解码器
FileAPI fileAPI = Feign.builder()
                .logger(new Slf4jLogger())
                .logLevel(Logger.Level.FULL)
                .target(FileAPI.class, "http://localhost:8080");
String fileName = "test.jpg";
Map<String, Object> queryMap = new HashMap<>();
queryMap.put("fileName", fileName);
Response response = fileAPI.download(queryMap);
if (response.status() == 200) {
    File downloadFile = new File("D:\\Downloads\\", fileName);
    FileUtils.copyInputStreamToFile(response.body().asInputStream(), downloadFile);
}

Using Spring Cloud Feign

When using Feign in Spring Framework to implement file uploads you need to depend on feign-form and feign-form-spring, these two modules are already included in “Spring Cloud Feign”, just add spring-cloud-starter-openfeign dependency.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
<!-- 集成Spring和Feign,包含了模块feign-form和feign-form-spring -->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-openfeign</artifactId>
    <version>3.0.2</version>
</dependency>

<!-- 文件操作工具类 -->
<dependency>
    <groupId>commons-io</groupId>
    <artifactId>commons-io</artifactId>
    <version>2.11.0</version>
</dependency>

Uploading files

Define the interface and configuration.

 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
@FeignClient(value = "FileAPI", url = "http://localhost:8080", configuration = FileUploadAPI.FileUploadAPIConfiguration.class)
public interface FileUploadAPI {

    /**
     * 上传单个文件
     * @param file
     * @return
     */
    @RequestMapping(value = "/test/upload/single", method = RequestMethod.POST, headers = "Content-Type=multipart/form-data")
    String upload(@RequestPart("file") MultipartFile file);

    /**
     * 上传多个文件
     * @param files
     * @return
     */
    @RequestMapping(value = "/test/upload/batch", method = RequestMethod.POST, headers = "Content-Type=multipart/form-data")
    String upload(@RequestPart("files") List<MultipartFile> files);

    class FileUploadAPIConfiguration {
        @Autowired
        private ObjectFactory<HttpMessageConverters> messageConverters;

        @Bean
        public Encoder feignEncoder () {
            return new SpringFormEncoder(new SpringEncoder(messageConverters));
        }

        @Bean
        public Logger feignLogger() {
            return new Slf4jLogger();
        }

        @Bean
        public Logger.Level feignLoggerLevel() {
            return Logger.Level.FULL;
        }
    }
}

Calling interface.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
// 上传单个文件
File file = new File("C:\\Users\\xxx\\Downloads\\test1.jpg");
FileInputStream fis = new FileInputStream(file);
MockMultipartFile mockMultipartFile = new MockMultipartFile("file", file.getName(), "image/jpg", fis);
this.fileUploadAPI.upload(mockMultipartFile);
fis.close();

// 上传多个文件
File file1 = new File("C:\\Users\\xxx\\Downloads\\test1.jpg");
File file2 = new File("C:\\Users\\xxx\\Downloads\\test2.jpg");
FileInputStream fis1 = new FileInputStream(file1);
FileInputStream fis2 = new FileInputStream(file2);
MockMultipartFile f1 = new MockMultipartFile("files", file1.getName(), "image/jpg", fis1);
MockMultipartFile f2 = new MockMultipartFile("files", file2.getName(), "image/jpg", fis2);
this.fileUploadAPI.upload(Arrays.asList(new MockMultipartFile[]{f1, f2}));
fis1.close();
fis2.close();

Download the file

Define the interface.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
@FeignClient(value = "FileDownloadAPI", url = "http://localhost:8080", configuration = FileDownloadAPI.FileDownloadAPIConfiguration.class)
public interface FileDownloadAPI {

    /**
     * 下载文件
     * @param fileName 文件名
     * @return
     */
    @RequestMapping(value = "/test/download/file", method = RequestMethod.GET)
    Response download(@RequestParam("fileName") String fileName);

    // 下载文件时返回值为Response对象,不需要设置解码器
    class FileDownloadAPIConfiguration {
        @Bean
        public Logger feignLogger() {
            return new Slf4jLogger();
        }

        @Bean
        public Logger.Level feignLoggerLevel() {
            return Logger.Level.FULL;
        }
    }
}

Calling interface.

1
2
3
4
5
String fileName = "test.jpg";
Response response = this.fileDownloadAPI.download(fileName);
File destFile = new File("D:\\Downloads\\", fileName);
// 使用org.apache.commons.io.FileUtils工具类将输入流中的内容转存到文件
FileUtils.copyInputStreamToFile(response.body().asInputStream(), destFile);

Summary

  1. Feign framework requires the integration module feign-form to support the message format of file uploads.
  2. Whether using Feign independently or using Spring Cloud Feign, the return value when downloading file must have a return value of type feign.Response.

Reference https://www.cnblogs.com/nuccch/p/15897806.html