27 Ağustos 2021 Cuma

SpringWebFlux FilePart Arayüzü

Giriş
Şu satırı dahil ederiz. WebFlux ile MultiPartFile kullanılamıyor
import org.springframework.http.codec.multipart.FilePart;
transferTo metodu
Örnek
Şöyle yaparız
import org.springframework.http.codec.multipart.FilePart;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestPart;

@RestController
@RequestMapping("upload")
public class UploadController {

  private final Path basePath = Paths.get("./src/main/resources/upload/");

  @PostMapping("file/single")
  public Mono<Void> upload(@RequestPart("user-name") String name,
                           @RequestPart("fileToUpload") Mono<FilePart> filePartMono){
    System.out.println("user : " + name);
    return filePartMono
      .doOnNext(fp -> System.out.println("Received File : " + fp.filename()))
      .flatMap(fp -> fp.transferTo(basePath.resolve(fp.filename())))
      .then();
  }

  @PostMapping("file/multi1")
  public Mono<Void> upload(@RequestPart("files") Flux<FilePart> partFlux){
    return partFlux
      .doOnNext(fp -> System.out.println(fp.filename()))
      .flatMap(fp -> fp.transferTo(basePath.resolve(fp.filename())))
      .then();
  }
}

Hiç yorum yok:

Yorum Gönder