feign上传与下载

在spring cloud 开发过程中,有个这样的需求,一个模块主要是管理素材,例如文件等等。而其他模块有时候需要和这个模块进行文件传输等,而模块与模块之间用通讯用Feign,在spring环境下用@FeignClient注解。为了避免遗忘,顾整理下。

Feign 上传文件

  • Feign 上传文件有现成的框架,网上也有不少文章,这只是稍做记录

框架地址:feign-form

maven 依赖:
1
2
3
4
5
6
7
8
9
10
11
<dependency>
<groupId>io.github.openfeign.form</groupId>
<artifactId>feign-form</artifactId>
<version>2.2.1</version>
</dependency>

<dependency>
<groupId>io.github.openfeign.form</groupId>
<artifactId>feign-form-spring</artifactId>
<version>2.2.1</version>
</dependency>
编写MultipartFile实现类
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78

import org.springframework.util.FileCopyUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;

public class InMemoryMultipartFile implements MultipartFile {

private final String name;
private final String originalFileName;
private final String contentType;
private final byte[] payload;

public InMemoryMultipartFile(File file) throws IOException {
this.originalFileName = file.getName();
this.payload = FileCopyUtils.copyToByteArray(file);
this.name = "file";
this.contentType = "application/octet-stream";
}

public InMemoryMultipartFile(String originalFileName, byte[] payload) {
this.originalFileName = originalFileName;
this.payload = payload;
this.name = "file";
this.contentType = "application/octet-stream";
}

public InMemoryMultipartFile(String name, String originalFileName, String contentType, byte[] payload) {
if (payload == null) {
throw new IllegalArgumentException("Payload cannot be null.");
}
this.name = name;
this.originalFileName = originalFileName;
this.contentType = contentType;
this.payload = payload;
}

@Override
public String getName() {
return name;
}

@Override
public String getOriginalFilename() {
return originalFileName;
}

@Override
public String getContentType() {
return contentType;
}

@Override
public boolean isEmpty() {
return payload.length == 0;
}

@Override
public long getSize() {
return payload.length;
}

@Override
public byte[] getBytes() throws IOException {
return payload;
}

@Override
public InputStream getInputStream() throws IOException {
return new ByteArrayInputStream(payload);
}

@Override
public void transferTo(File dest) throws IOException, IllegalStateException {
new FileOutputStream(dest).write(payload);
}

}
编写接口
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

@FeignClient(value = "material", configuration = FuyhuiMateClient.MultipartSupportConfig.class)
public interface FuyhuiMateClient {

@PostMapping("/uploadFile")
@Headers("Content-Type: multipart/form-data")
ResponseMO uploadFile(@RequestPart("file") MultipartFile file);


class MultipartSupportConfig {

@Autowired
ObjectFactory<HttpMessageConverters> messageConverters;

@Bean
@Primary
@Scope("prototype")
public Encoder multipartFormEncoder() {
return new SpringFormEncoder(new SpringEncoder(messageConverters));
}
}
}

上传处理Controller

上传处理Controll和平时的SpringMVC上传是一样,这里就不在记录了。

Feign 下载文件

Google、Baidu了很久,也在Github上找了,找了很就也没找到解决方案,不