[英]How to upload multiple files using Webflux?
如何使用 Webflux 上传多个文件?
我发送内容类型为multipart/form-data
请求,正文包含一个部分,其值为一组文件。
要处理单个文件,我按如下方式执行:
Mono<MultiValueMap<String, Part> body = request.body(toMultipartData());
body.flatMap(map -> FilePart part = (FilePart) map.toSingleValueMap().get("file"));
但是如何为多个文件做到这一点?
附注。 还有另一种方法可以在 webflux 中上传一组文件吗?
我已经找到了一些解决方案。 假设我们发送一个包含我们文件的参数文件的 http POST 请求。
注意响应是任意的
带有 RequestPart 的 RestController
@PostMapping("/upload") public Mono<String> process(@RequestPart("files") Flux<FilePart> filePartFlux) { return filePartFlux.flatMap(it -> it.transferTo(Paths.get("/tmp/" + it.filename()))) .then(Mono.just("OK")); }
带有 ModelAttribute 的 RestController
@PostMapping("/upload-model") public Mono<String> processModel(@ModelAttribute Model model) { model.getFiles().forEach(it -> it.transferTo(Paths.get("/tmp/" + it.filename()))); return Mono.just("OK"); } class Model { private List<FilePart> files; //getters and setters }
HandlerFunction 的功能方式
public Mono<ServerResponse> upload(ServerRequest request) { Mono<String> then = request.multipartData().map(it -> it.get("files")) .flatMapMany(Flux::fromIterable) .cast(FilePart.class) .flatMap(it -> it.transferTo(Paths.get("/tmp/" + it.filename()))) .then(Mono.just("OK")); return ServerResponse.ok().body(then, String.class); }
您可以使用 Flux 迭代 hashmap 并返回 Flux
Flux.fromIterable(hashMap.entrySet())
.map(o -> hashmap.get(o));
它将作为带有文件部分的数组发送
关键是使用toParts而不是toMultipartData ,后者更简单。 这是与RouterFunctions一起使用的示例。
private Mono<ServerResponse> working2(final ServerRequest request) {
final Flux<Void> voidFlux = request.body(BodyExtractors.toParts())
.cast(FilePart.class)
.flatMap(filePart -> {
final String extension = FilenameUtils.getExtension(filePart.filename());
final String baseName = FilenameUtils.getBaseName(filePart.filename());
final String format = LocalDateTime.now().format(DateTimeFormatter.BASIC_ISO_DATE);
final Path path = Path.of("/tmp", String.format("%s-%s.%s", baseName, format, extension));
return filePart.transferTo(path);
});
return ServerResponse
.ok()
.contentType(APPLICATION_JSON_UTF8)
.body(voidFlux, Void.class);
}
希望对你有帮助
@PostMapping(value = "/upload", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public JSON fileUpload(@RequestPart FilePart file)throws Exception{
OSS ossClient = new OSSClientBuilder().build(APPConfig.ENDPOINT, APPConfig.ALI_ACCESSKEYID, APPConfig.ALI_ACCESSSECRET);
File f = null;
String url;
try {
String suffix = file.filename();
String fileName = "images/" + file.filename();
Path path = Files.createTempFile("tempimg", suffix.substring(1, suffix.length()));
file.transferTo(path);
f = path.toFile();
ossClient.putObject(APPConfig.BUCKETNAME, fileName, new FileInputStream(f));
Date expiration = new Date(System.currentTimeMillis() + 3600L * 1000 * 24 * 365 * 10);
url = ossClient.generatePresignedUrl(APPConfig.BUCKETNAME, fileName, expiration).toString();
}finally {
f.delete();
ossClient.shutdown();
}
return JSONUtils.successResposeData(url);
}
以下是使用 WebFlux 上传多个文件的工作代码。
@RequestMapping(value = "upload", method = RequestMethod.POST)
Mono<Object> upload(@RequestBody Flux<Part> parts) {
return parts.log().collectList().map(mparts -> {
return mparts.stream().map(mmp -> {
if (mmp instanceof FilePart) {
FilePart fp = (FilePart) mmp;
fp.transferTo(new File("c:/hello/"+fp.filename()));
} else {
// process the other non file parts
}
return mmp instanceof FilePart ? mmp.name() + ":" + ((FilePart) mmp).filename() : mmp.name();
}).collect(Collectors.joining(",", "[", "]"));
});
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.