为了账号安全,请及时绑定邮箱和手机立即绑定

发送收到的 spring FilePart 而不保存

发送收到的 spring FilePart 而不保存

POPMUISE 2021-10-28 09:17:51
我需要使用 WebClient 将 RestController 中收到的 FilePart 发送到 API,我该怎么做?找到一个示例,将图像保存到磁盘。private static String UPLOAD_ROOT = "C:\\pics\\";public Mono<Void> checkInTest(@RequestPart("photo") Flux<FilePart> photoParts,                              @RequestPart("data") CheckInParams params, Principal principal) {    return saveFileToDisk(photoParts);}private Mono<Void> saveFileToDisk(Flux<FilePart> parts) {    return parts            .log("createImage-files")            .flatMap(file -> {                Mono<Void> copyFile = Mono.just(Paths.get(UPLOAD_ROOT, file.filename()).toFile())                        .log("createImage-picktarget")                        .map(destFile -> {                            try {                                destFile.createNewFile();                                return destFile;                            } catch (IOException e) {                                throw new RuntimeException(e);                            }                        })                        .log("createImage-newfile")                        .flatMap(file::transferTo)                        .log("createImage-copy");                return Mono.when(copyFile)                        .log("createImage-when");            })            .log("createImage-flatMap")            .then()            .log("createImage-done");}然后再次阅读并发送到另一个服务器.map(destFile -> {        MultiValueMap<String, Object> map = new LinkedMultiValueMap<>();        try {            map.set("multipartFile", new ByteArrayResource(FileUtils.readFileToByteArray(destFile)));        } catch (IOException ignored) {        }        map.set("fileName", "test.txt");        WebClient client = WebClient.builder().baseUrl("http://localhost:8080").build();        return client.post()                .uri("/upload")                .contentType(MediaType.MULTIPART_FORM_DATA)                .syncBody(map)                .exchange(); //todo handle errors???    }).then()有没有办法避免保存文件?
查看完整描述

1 回答

  • 1 回答
  • 0 关注
  • 257 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信