spring Boot结合openfeign 文件上传 步骤

这里写自定义目录标题

添加依赖`

<!-- 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);
    }
  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值