Books suggestion
@GetMapping("/unable_files")
public Flux<FileInfo> wrongListFiles() throws IOException {
return Flux.fromIterable(Files.newDirectoryStream(Paths.get("C:/Python33")))
.map(path -> new FileInfo(path.getFileName().toString(), path.toAbsolutePath().toString()));
}
But it always encounter error
2020-05-17 02:45:23.134 ERROR 6980 --- [ctor-http-nio-2] a.w.r.e.AbstractErrorWebExceptionHandler : [3b6f7b07-1] 500 Server Error for HTTP GET "/unable_files"
java.lang.IllegalStateException: Iterator already obtained
at sun.nio.fs.WindowsDirectoryStream.iterator(WindowsDirectoryStream.java:117) ~[na:1.8.0_92]
Suppressed: reactor.core.publisher.FluxOnAssembly$OnAssemblyException:
Error has been observed at the following site(s):
|_ checkpoint ? Handler com.example.demo.FileController#wrongListFiles() [DispatcherHandler]
|_ checkpoint ? HTTP GET "/unable_files" [ExceptionHandlingWebHandler]
Stack trace:
at sun.nio.fs.WindowsDirectoryStream.iterator(WindowsDirectoryStream.java:117) ~[na:1.8.0_92]
at reactor.core.publisher.FluxIterable.subscribe(FluxIterable.java:79) [reactor-core-3.3.5.RELEASE.jar:3.3.5.RELEASE]
Fixed
- FIleInfo.java
package com.example.demo;
import lombok.AllArgsConstructor;
import lombok.Data;
@Data
@AllArgsConstructor
public class FileInfo {
private String name;
private String path;
}
- list files
@GetMapping("/files")
public Flux<FileInfo> listFiles() throws IOException {
return Flux.fromStream(Files.list(Paths.get("C:/Python33")))
.map(path -> new FileInfo(path.getFileName().toString(), path.toAbsolutePath().toString()));
}
Client
$ curl -s http://localhost:8080/files
[{"name":"DLLs","path":"C:\\Python33\\DLLs"},{"name":"Doc","path":"C:\\Python33\\Doc"},{"name":"include","path":"C:\\Python33\\include"},{"name":"isaac","path":"C:\\Python33\\isaac"},{"name":"Lib","path":"C:\\Python33\\Lib"},{"name":"libs","path":"C:\\Python33\\libs"},{"name":"LICENSE.txt","path":"C:\\Python33\\LICENSE.txt"},{"name":"NEWS.txt","path":"C:\\Python33\\NEWS.txt"},{"name":"python.exe","path":"C:\\Python33\\python.exe"},{"name":"pythonw.exe","path":"C:\\Python33\\pythonw.exe"},{"name":"q.py","path":"C:\\Python33\\q.py"},{"name":"README.txt","path":"C:\\Python33\\README.txt"},{"name":"say.py","path":"C:\\Python33\\say.py"},{"name":"Scripts","path":"C:\\Python33\\Scripts"},{"name":"tcl","path":"C:\\Python33\\tcl"},{"name":"test.py","path":"C:\\Python33\\test.py"},{"name":"Tools","path":"C:\\Python33\\Tools"},{"name":"__pycache__","path":"C:\\Python33\\__pycache__"}]
沒有留言:
張貼留言