我正在尝试获取所有我保存在spring resources / templates中的template文件夹中的模板。以下是我正在尝试的操作,我正在尝试使用/ getTeamplateList进行访问,但没有给出任何操作
@RequestMapping(value = "/getTemplateList", produces = "application/json")
public List getTemplates() throws IOException {
List s = new ArrayList();
File[] resourceFolderFiles = getResourceFolderFiles("classpath:/templates/");
s.addAll(Arrays.asList(resourceFolderFiles));
return s;
}
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();
}
答案 0 :(得分:0)
您可以通过以下方式进行操作:
@Autowired
private ResourceLoader resourceLoader;
final List<String> result = Arrays.stream(loadResources()).map(resource -> resource.getFilename()).collect(Collectors.toList());
private Resource[] loadResources() throws IOException {
return ResourcePatternUtils.getResourcePatternResolver(resourceLoader).getResources("classpath:/templates/*.html");
}
这将打印
[
"template1.html",
"template2.html"
...
]
在响应中。