在使用 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";
});
}
注意:以上代码,为作者个人观点。