我已经在资源中创建了assets文件夹,我想在assets文件夹中显示图片名称列表。我想在assets文件夹中显示图片名称的列表。通过点击图像名称,它应该打开特定的图像。我可以单独访问图片,但如何以文件浏览器的方式显示所有图片?
你可以使用 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();
}
}