添加依赖`
<!-- Spring Boot Web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- Spring Cloud OpenFeign -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<!-- Feign 文件上传支持 -->
<dependency>
<groupId>io.github.openfeign.form</groupId>
<artifactId>feign-form</artifactId>
<version>最新版本号</version> <!-- 请替换为最新版本号 -->
</dependency>
<dependency>
<groupId>io.github.openfeign.form</groupId>
<artifactId>feign-form-spring</artifactId>
<version>最新版本号</version> <!-- 请替换为最新版本号 -->
</dependency>
<!-- 其他必要的依赖 -->
## 配置Feign客户端
@FeignClient(name = "file-service", configuration = FileUploadConfig.class)
public interface FileUploadClient {
@PostMapping(value = "/upload", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
ResponseEntity<String> uploadFile(@RequestPart("file") MultipartFile file);
}
配置Feign的文件上传支持
/**
* @author yuyingsheng
*/
@FeignClient(name = "fileUploadClient", url = "http://localhost:8080")
public interface FileUploadClient {
UUIDGenerator UUID_GENERATOR = new UUIDGenerator();
@RequestMapping(method = RequestMethod.POST, value = "/upload", consumes = MediaType.MULTIPART_FORM_DATA_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
FileInfo upload(MultipartFile file);
default FileInfo upload(InputStream inputStream, String fileExtension) throws IOException {
MultipartFile multipartFile = new FlyFlowMultipartFile(
"file", // 这里的"file"是请求中表单字段的名称,根据实际情况调整
UUID_GENERATOR.next() + "." + fileExtension,
"application/octet-stream",
inputStream
);
return upload(multipartFile);
}
default FileInfo upload(File file) throws IOException {
String fileName = file.getName();
System.out.println("fileName" + fileName);
String extension = FilenameUtils.getExtension(file.getPath());
System.out.println("extension" + extension);
FileInputStream inputStream = new FileInputStream(file);
MultipartFile multipartFile = new FlyFlowMultipartFile(
"file", // 这里的"file"是请求中表单字段的名称,根据实际情况调整
fileName,
"application/octet-stream",
inputStream
);
return upload(multipartFile);
}