我正在尝试从Java Spring后端检索文件的文件夹,但找不到正确的方法。我在做什么错了?
我设法像这样检索单个文件:
@CrossOrigin(origins = "http://localhost:3000")
@GetMapping(value = "gallery/test", produces = MediaType.IMAGE_JPEG_VALUE)
public @ResponseBody byte[] getImage() throws IOException {
return IOUtils.toByteArray(getClass()
.getResourceAsStream("/gallery/test/images.jpg"));
}
并且试图修改代码,使其应返回Byte类型的ArrayList
private static File[] getResourceFolderFiles (String folder) {
ClassLoader loader = Thread.currentThread().getContextClassLoader();
URL url = loader.getResource(folder);
String path = url.getPath();
return new File(path).listFiles();
...
@CrossOrigin(origins = "http://localhost:3000")
@GetMapping(value = "gallery/test", produces = MediaType.IMAGE_JPEG_VALUE)
public @ResponseBody
ArrayList<byte[]> getImage() throws IOException {
ArrayList<byte[]> files = new ArrayList();
for (File f : getResourceFolderFiles("/gallery/test/")) {
System.out.println(f.getAbsolutePath());
files.add(IOUtils.toByteArray(getClass()
.getResourceAsStream(f.getAbsolutePath())));
}
return files;
}
}