服务于春秋战国时期的图像列表

问题描述 投票:0回答:1

我已经在资源中创建了assets文件夹,我想在assets文件夹中显示图片名称列表。我想在assets文件夹中显示图片名称的列表。通过点击图像名称,它应该打开特定的图像。我可以单独访问图片,但如何以文件浏览器的方式显示所有图片?

image spring-boot static
1个回答
0
投票

你可以使用 ResourcePatternResolver:

@Controller
@RequestMapping("/assets")
public class AssetController {

    @Autowired
    private ResourcePatternResolver resolver;

    @GetMapping("")
    @ResponseBody
    public String resources() throws IOException {
        final String root = resolver.getResource("classpath:/static/assets").getURI().toString();

        final Resource[] resources = resolver
            .getResources("classpath:/static/assets/**/*.png");
        final List<String> fileNames = Stream.of(resources)
            .filter(Resource::isFile)
            .map(r -> {
                try {
                    return r.getURI().toString().replace(root, "");
                } catch (final IOException e) {
                    throw new IOError(e);
                }
            })
            .collect(Collectors.toList());

        final StringBuilder html = new StringBuilder();
        html.append("<html>");
        html.append("<ul>");
        for (final String fileName : fileNames) {
            html.append("<li>");
            html.append("<a href=\"/assets" + fileName + "\">" + fileName + "</a>");
            html.append("</li>");
        }
        html.append("</ul>");
        html.append("</html>");
        return html.toString();
    }

}
© www.soinside.com 2019 - 2024. All rights reserved.