【发布时间】:2020-04-18 15:03:23
【问题描述】:
下面的代码是遍历文件夹中的文件列表,以便在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
无法更正此问题。请帮忙。
【问题讨论】:
-
请发布完整的堆栈跟踪
-
你能确定你只在这个粘贴的代码中使用
Files.newDirectoryStream(Paths.get(VIDEO_FILE_LOCATION)吗?像这个流可以迭代一次。如果你需要再次迭代它,那么你必须再次写Files.newDirectoryStream(Paths.get(VIDEO_FILE_LOCATION)这个。 -
是的,我在 CommandLineRunner 中使用它来填充数据库中的文件名,没有其他地方。在搜索中,我也遇到了关于 DirectoryStream 仅迭代一次的答案。但是在这里,只有一次迭代对吗?
-
对不起兄弟,我无法从头到尾重现问题。甚至我也尝试在 commanlinerunner 中实现它。但从我这边开始一切都很顺利。
-
你的意思是类似的代码在你最后工作?! .也许有一天我会找出原因!感谢您的帮助。
标签: spring-boot spring-webflux