Spring boot Files.newDirectoryStream抛出java.lang.IllegalStateException:已获得迭代器

Spring boot Files.newDirectoryStream抛出java.lang.IllegalStateException:已获得迭代器,spring-boot,spring-webflux,Spring Boot,Spring Webflux,下面的代码是遍历文件夹中的文件列表,以便在db中创建数据,但抛出IllegalStateException:Iterator已获得异常 Flux.fromIterable(Files.newDirectoryStream(Paths.get(VIDEO_FILE_LOCATION))) .map(file -> new VideoFile(file.getFileName() .toStr

下面的代码是遍历文件夹中的文件列表,以便在db中创建数据,但抛出IllegalStateException:Iterator已获得异常

Flux.fromIterable(Files.newDirectoryStream(Paths.get(VIDEO_FILE_LOCATION)))
          .map(file -> new VideoFile(file.getFileName()
                                         .toString()))
          .subscribe(f -> videoRepository.save(f));
满栈

java.lang.IllegalStateException: Failed to execute CommandLineRunner
    at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:787) ~[spring-boot-2.2.6.RELEASE.jar:2.2.6.RELEASE]
    at org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:768) ~[spring-boot-2.2.6.RELEASE.jar:2.2.6.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:322) ~[spring-boot-2.2.6.RELEASE.jar:2.2.6.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1226) ~[spring-boot-2.2.6.RELEASE.jar:2.2.6.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1215) ~[spring-boot-2.2.6.RELEASE.jar:2.2.6.RELEASE]
    at com.kalarikkal.KalarikkalApplication.main(Kalarikkalpplication.java:30) ~[main/:na]
Caused by: reactor.core.Exceptions$ErrorCallbackNotImplemented: java.lang.IllegalStateException: Iterator already obtained
Caused by: java.lang.IllegalStateException: Iterator already obtained

无法更正此问题。请帮忙。

根据123的建议,我已将代码更改如下。它起作用了

    @Bean
      CommandLineRunner init(MongoOperations mongoOperations)
      {
        return args ->
        {
          mongoOperations.dropCollection(VideoFile.class);
    Path myDir = Paths.get(VIDEO_FILE_LOCATION);
          Stream<Path> directorStream = Files.list(myDir);
          Flux.fromStream(directorStream)
              .doOnNext(path -> mongoOperations.insert(new VideoFile(path.getFileName()
                                                                         .toString())))
              .subscribe();
        };
    }
@Bean
CommandLineRunner初始化(MongoOperations MongoOperations)
{
返回参数->
{
mongoOperations.dropCollection(VideoFile.class);
Path myDir=Path.get(视频文件位置);
Stream directorStream=Files.list(myDir);
Flux.fromStream(directorStream)
.doOnNext(path->mongoOperations.insert(新视频文件(path.getFileName)()
.toString()))
.subscribe();
};
}

post with full stack trace请确保您使用的是
Files.newDirectoryStream(path.get(VIDEO\u FILE\u LOCATION)
仅在粘贴的代码中?就像此流可以只迭代一次。如果需要再次迭代,则必须编写
Files.newDirectoryStream(path.get(VIDEO\u FILE\u LOCATION)
这又是一次。是的,我在CommandLineRunner中使用它来填充数据库中的文件名,而不是其他地方。在搜索时,我也发现了关于DirectoryStream只迭代了一次的答案。但这里只有一次迭代,对吗?对不起,兄弟,我无法从头开始重新创建问题。甚至我也尝试在co中实现它mmanlinerunner。但从我这边开始,一切都很顺利。你是说类似的代码在你这边也能工作?!也许有一天我会找到原因的!!。谢谢你的帮助。
subscribe()
正在阻止och读取文件系统正在阻止,因此,如果此代码在webflux应用程序中,这是一种不好的做法,可能会使应用程序崩溃。我正在CommandLineRunner中使用此块获取文件名并以反应方式插入Mongo。我已更改上述代码,现在正在使用MongoOperations.MongoOperations.insert(新视频文件(path.getFileName().toString());我在第一个代码中遇到了与您相同的错误。搜索了一段时间后,没有找到任何解决方案。我像您一样更改了代码,它可以工作。