Java Spring,getMapping返回文件的文件夹

时间:2020-04-20 14:26:59

标签: java spring-mvc

我正在尝试从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;

    }
}

0 个答案:

没有答案