列出 Spring 引导中模板目录中的文件
List Files from Templates Directory in Spring Boot
我想生成博客文章概览。为此,我想从 Spring Boot 存储其模板的资源文件夹中的模板文件夹内的文件夹中读取 html 文件。
我试过了,但没有 return 错误,但也没有列出任何文件。
这里怎么走?
谢谢
@Controller
public class Route {
@Autowired
private ResourceLoader resourceLoader;
@RequestMapping("/")
public String home() throws IOException {
final String path = "templates/blog";
final Resource res = resourceLoader.getResource("templates/blog");
try (final BufferedReader reader = new BufferedReader(new InputStreamReader(res.getInputStream()))) {
reader.lines().forEachOrdered(System.out::println);
}
return "blog/a";
}
}
您应该可以使用 NIO2 来实现。
为了让 NIO2 工作,它需要 concept of FileSystem,并且可以从 jar URI 创建一个。那么这个文件系统就可以用Files/Paths了。
下面的代码包含两个分支 - 第一个处理从 Jar 内部加载文件 ,第二个分支 - 当代码运行 从 IDE 或通过 "mvn spring-boot:run"
.
所有流都通过 try-with-resources 使用,因此它们将自动关闭。
查找函数从文件系统的顶部开始,递归搜索 html 个文件。
public static void readFile(String location) throws URISyntaxException {
URI uri = Objects.requireNonNull(ReadFromJar.class.getClassLoader().getResource(location)).toURI();
if (uri.getScheme().equals("jar")) { //inside jar
try (FileSystem fs = FileSystems.newFileSystem(uri, Collections.emptyMap())) { //build a new FS that represents the jar's contents
Files.find(fs.getPath("/"), 10, (path, fileAttr) -> // control the search depth (e.g. 10)
fileAttr.isRegularFile() //match only files
&& path.toString().contains("blog") //match only files in paths containing "blog"
&& path.getFileName().toString().matches(".*\.html")) // match only html files
.forEach(ReadFromJar::printFileContent);
} catch (IOException ex) {
ex.printStackTrace();
}
}
else { //from IDE or spring-boot:run
final Path path = Paths.get(uri);
try (DirectoryStream<Path> dirStream = Files.newDirectoryStream(path)) {
dirStream.forEach(ReadFromJar::printFileContent);
} catch (IOException e) {
e.printStackTrace();
}
}
}
private static void printFileContent(final Path file) {
try {
System.out.println("Full path: " + file.toAbsolutePath().toString());
Files.lines(file).forEach(System.out::println);
} catch (IOException e) {
e.printStackTrace();
}
}
@Controller
public class Route {
@Value("classpath:templates/blog/*")
private Resource[] resources;
@RequestMapping("/")
public String home() throws IOException {
for (final Resource res : resources) {
System.out.println(res.getFilename());
}
return "blog/a";
}
}
对我有用。
我想生成博客文章概览。为此,我想从 Spring Boot 存储其模板的资源文件夹中的模板文件夹内的文件夹中读取 html 文件。
我试过了,但没有 return 错误,但也没有列出任何文件。
这里怎么走?
谢谢
@Controller
public class Route {
@Autowired
private ResourceLoader resourceLoader;
@RequestMapping("/")
public String home() throws IOException {
final String path = "templates/blog";
final Resource res = resourceLoader.getResource("templates/blog");
try (final BufferedReader reader = new BufferedReader(new InputStreamReader(res.getInputStream()))) {
reader.lines().forEachOrdered(System.out::println);
}
return "blog/a";
}
}
您应该可以使用 NIO2 来实现。
为了让 NIO2 工作,它需要 concept of FileSystem,并且可以从 jar URI 创建一个。那么这个文件系统就可以用Files/Paths了。
下面的代码包含两个分支 - 第一个处理从 Jar 内部加载文件 ,第二个分支 - 当代码运行 从 IDE 或通过 "mvn spring-boot:run"
.
所有流都通过 try-with-resources 使用,因此它们将自动关闭。
查找函数从文件系统的顶部开始,递归搜索 html 个文件。
public static void readFile(String location) throws URISyntaxException {
URI uri = Objects.requireNonNull(ReadFromJar.class.getClassLoader().getResource(location)).toURI();
if (uri.getScheme().equals("jar")) { //inside jar
try (FileSystem fs = FileSystems.newFileSystem(uri, Collections.emptyMap())) { //build a new FS that represents the jar's contents
Files.find(fs.getPath("/"), 10, (path, fileAttr) -> // control the search depth (e.g. 10)
fileAttr.isRegularFile() //match only files
&& path.toString().contains("blog") //match only files in paths containing "blog"
&& path.getFileName().toString().matches(".*\.html")) // match only html files
.forEach(ReadFromJar::printFileContent);
} catch (IOException ex) {
ex.printStackTrace();
}
}
else { //from IDE or spring-boot:run
final Path path = Paths.get(uri);
try (DirectoryStream<Path> dirStream = Files.newDirectoryStream(path)) {
dirStream.forEach(ReadFromJar::printFileContent);
} catch (IOException e) {
e.printStackTrace();
}
}
}
private static void printFileContent(final Path file) {
try {
System.out.println("Full path: " + file.toAbsolutePath().toString());
Files.lines(file).forEach(System.out::println);
} catch (IOException e) {
e.printStackTrace();
}
}
@Controller
public class Route {
@Value("classpath:templates/blog/*")
private Resource[] resources;
@RequestMapping("/")
public String home() throws IOException {
for (final Resource res : resources) {
System.out.println(res.getFilename());
}
return "blog/a";
}
}
对我有用。