webflux 获取文件,并将文件转换MultipartFile

在使用 Spring WebFlux 框架中,你可以使用 org.springframework.http.codec.multipart.FilePart 类来获取上传的文件。
比如:

import org.springframework.http.codec.multipart.FilePart;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Mono;

@RestController
public class FileController {

    @PostMapping("/upload")
    public Mono<String> uploadFile(@RequestPart("file") FilePart filePart) {
        // 处理上传的文件
        // 你可以通过 filePart 类的方法获取文件信息或进行文件处理操作
        // 比如保存文件到硬盘、读取文件内容等

        return Mono.just("File uploaded successfully!");
    }
}

在上述示例中,我们使用 @RequestPart 注解将请求中名为 “file” 的部分映射到 FilePart 类型的参数上。然后,你可以在方法体内对文件进行处理。例如,你可以通过调用 filePart.filename() 方法获取文件名,并将文件保存到指定位置。

需要注意的是,必须将 HTTP 请求的 Content-Type 设置为 multipart/form-data,以便支持文件上传。

接下来,我们将FilePart转成 MultipartFile,
可以自定义一个MultipartFile实现类:

import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;

public class CustomMultipartFile implements MultipartFile {

    private final String name;

    private String originalFilename;

    @Nullable
    private String contentType;

    private final byte[] content;

    public CustomMultipartFile(String name, @Nullable byte[] content) {
        this(name, "", null, content);
    }

    public CustomMultipartFile(String name, InputStream contentStream) throws IOException {
        this(name, "", null, FileCopyUtils.copyToByteArray(contentStream));
    }

    public CustomMultipartFile(
            String name, @Nullable String originalFilename, @Nullable String contentType, @Nullable byte[] content) {

        Assert.hasLength(name, "Name must not be null");
        this.name = name;
        this.originalFilename = (originalFilename != null ? originalFilename : "");
        this.contentType = contentType;
        this.content = (content != null ? content : new byte[0]);
    }

    public CustomMultipartFile(
            String name, @Nullable String originalFilename, @Nullable String contentType, InputStream contentStream)
            throws IOException {

        this(name, originalFilename, contentType, FileCopyUtils.copyToByteArray(contentStream));
    }


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

    @Override
    public String getOriginalFilename() {
        return this.originalFilename;
    }

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

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

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

    @Override
    public byte[] getBytes() throws IOException {
        return this.content;
    }

    @Override
    public InputStream getInputStream() throws IOException {
        return new ByteArrayInputStream(this.content);
    }

    @Override
    public void transferTo(File dest) throws IOException, IllegalStateException {
        FileCopyUtils.copy(this.content, dest);
    }

}

在将FilePart转成 CustomMultipartFile,这里需要指定文件的具体contentType 类型

private <T> Mono<T> toMultipartFile(FilePart filePart, Function<MultipartFile,T> function){
        return DataBufferUtils.join(filePart.content()).flatMap(dataBuffer -> {
            byte[] bytes = new byte[dataBuffer.readableByteCount()];
            dataBuffer.read(bytes);
            DataBufferUtils.release(dataBuffer); //释放内存
            return Mono.just(bytes);
        }).map(fileBytes -> {
            // 处理上传的文件字节数组
            String fileName = filePart.filename();
            //文件contentType 类型
            String contentType = "text/plain";
            return function.apply(new CustomMultipartFile(fileName, fileName, contentType, fileBytes));
        });
    }

最后,

	@PostMapping("/api/test")
    public Mono<String> test11(@RequestPart("file") FilePart filePart) {
        return toMultipartFile(filePart,multipartFile -> {
            //处理multipartFile 文件逻辑
            return "success";
        });
    }

注意:以上代码,为作者个人观点。

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值