如何从Spring Flux平面图操作返回对象



我希望在保存文件后返回Mono.just(file.getAbsolutePath(((。以下是我的代码:

public Mono<String> save(Mono<FilePart> filePartMono) {
Mono<String> monoString = filePartMono.flatMap(filePart -> {
File file = new File(filePart.filename());
if (file.exists()) {
file.delete();
LOG.info("existing file deleted: {}", file.getAbsolutePath());
}
Mono<Void> mono = filePart.transferTo(file);
LOG.info("file saved: {}", file.getAbsolutePath());
return Mono.just(file.getAbsolutePath());
}).thenReturn("hello");
return monoString;

现在我正在返回一个";你好";。有没有一种方法可以返回文件.getAbsolutePath((,而不是我的save((方法中的字符串?

我认为可以这样做:

public Mono<String> save(Mono<FilePart> filePartMono) {
return filePartMono.flatMap(filePart -> {
File file = new File(filePart.filename());
if (file.exists()) {
file.delete();
log.info("existing file deleted: {}", file.getAbsolutePath());
}
return filePart.transferTo(file)
.doOnNext(v -> {
log.info("file saved: {}", file.getAbsolutePath());
}).thenReturn(file.getAbsolutePath());
});
}

最新更新