【发布时间】:2019-05-31 10:20:22
【问题描述】:
我在 src/main/resources/static/css/ 下有一个名为 my.css 的文件,我正在尝试将其作为字符串加载,但我终生无法让 Spring 找到这个文件据说已经在类路径中静态加载了。
private String getCss(String cssFileName) {
try {
File file = new ClassPathResource("classpath:" + cssFileName + ".css").getFile();
return new String(Files.readAllBytes(Paths.get(file.getPath())));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return "css not found";
}
}
我尝试了各种 webconfig、资源加载器、路径和模式,但我就是无法让它工作。如何在我的资源中找到文件?我希望某种 Resources.getResource("name.type") 类型的东西已经有一棵树,其中列出了资源文件夹中的所有资源。
【问题讨论】:
-
您是否尝试过类似的方法:
ResourceLoader.class.getResourceAsStream("/static/css/my.css");其中ResourceLoader是您的类路径中的任何类?
标签: java spring spring-boot spring-mvc file-io