【问题标题】:How to read all files from the resource folder of a SpringBoot app?如何从 SpringBoot 应用程序的资源文件夹中读取所有文件?
【发布时间】:2019-02-08 14:05:03
【问题描述】:

我有一个 Spring Boot v2.1.2.RELEASE 应用程序。 我有这个文件夹../src/main/resources/icons/svg/white/

我正在尝试列出该文件夹的所有文件,但该文件夹似乎不存在

@SpringBootApplication
public class SvgManagerApplication implements CommandLineRunner {
    public static void main(String[] args) {
        SpringApplication.run(SvgManagerApplication.class, args);
    }

    @Override
    public void run(String... args) throws Exception {      
        try {
            File folder = new File("icons/svg/white/");
            listFilesForFolder(folder);
        } catch (IOException ex) {
            System.out.println(ex.getMessage());
        }
    }

    public void listFilesForFolder(final File folder) {
        for (final File fileEntry : folder.listFiles()) {
            if (fileEntry.isDirectory()) {
                listFilesForFolder(fileEntry);
            } else {
                System.out.println(fileEntry.getName());
            }
        }
    }    
}

【问题讨论】:

    标签: java spring spring-boot spring-mvc


    【解决方案1】:

    使用ResourceUtils

    File folder= ResourceUtils.getFile("classpath:icons/svg/white/");
    

    【讨论】:

      猜你喜欢
      • 2018-04-05
      • 1970-01-01
      • 2020-03-03
      • 2018-11-02
      • 2017-05-18
      • 2018-11-12
      • 1970-01-01
      • 2020-01-20
      相关资源
      最近更新 更多