【问题标题】:How to get number of resources in directory in spring?如何在春季获取目录中的资源数量?
【发布时间】:2015-01-20 09:53:32
【问题描述】:
@Value("file:" + "${d:/my/dir/}")
private Resource dir;

如何获取该资源目录中的文件数?不幸的是,Resource 接口不提供任何 getFiles() 方法...

【问题讨论】:

  • Java resource as file 的可能副本 在发布之前稍稍在网站上搜索一下,答案可能已经存在。特别是this answer
  • 我知道原生 java 文件包有可能。但是我明确要求使用springResource,说不定也有机会通过spring直接获取dir内容。
  • 我的应用程序,我错过了 spring Resource 部分,但如果它是 Resource 指向的目录,那么您可以简单地使用 getFile() 来获取 File ref。在 File 上,您可以调用 isDirectory 和 list()。至少基于Resource接口,似乎是这样的

标签: java spring


【解决方案1】:

基于Resource接口的this version,可以调用getFile

File theDir = dir.getFile();
if (theDir.isDirectory()) {
    theDir.list();
}

如果您将其与 propertyplaceholderconfigurer 结合使用,您可能会得到您想要的:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-03
    • 2023-03-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多