java - 从 Spring Boot 中读取文件夹会导致 FileNotFound 异常
问题描述
我正在尝试将我的 spring 应用程序移动到 spring boot
在我早期的 spring 应用程序中,我有两个模块 core 和 war。在核心中,该模块被添加为战争中的依赖项。在核心模块中,我读取了资源中指定的文件夹(遍历嵌套文件夹并加载文件)。当我以前将它作为 tomcat 运行时,这曾经可以工作。
现在我已将我的应用程序迁移到 Spring Boot。读取文件夹在 IDE 中有效,但是当我尝试将其运行为
java -jar dnow/war/target/war.jar
它给了我一个错误
java.io.FileNotFoundException: class path resource [datasources] cannot be resolved to absolute file path because it does not reside in the file system:
jar:file:/Users/kkadam/Work/repos/Project/dnow/war/target/war.jar!/BOOT-INF/classes!/datasources
我的代码如下
public static File[] getListOfFiles(String folder) {
File file;
File[] listFiles = null;
String property = Utils.getConfDirectoryPath();
if (!Utils.isEmpty(property)) {
try {
if (!property.endsWith("/"))
property = property + "/";
file = new File(property + folder);
listFiles = file.listFiles();
} catch (Exception e) {
logger.info("Folder " + folder + "does not found. Reading from resources");
}
}
if (listFiles != null)
return listFiles;
else {
return listFilesFromClassPath(folder);
}
}
public static File[] listFilesFromClassPath(String folder) {
File file;
URL url = FileUtils.class.getClassLoader().getResource(folder);
file = new File(url.getPath());
return file.listFiles();
}
解决方案
推荐阅读
- http - 通过 indy “发布”失败,而“获取”正在工作,使用 xe2、https 和 windows 身份验证
- spring - 查找具有此条目最大版本的条目的列表
- apache - 尝试为两个不同的端口配置 apache2 Web 服务器
- .net - 在 .NET core 2.1 中获取服务器绝对路径
- corda - 节点未从 Cordite 仪表板中删除
- java - SSH 命令终止序列
- python - 通过重复加法进行二进制乘法
- java - 无法在 Spring Boot 中为每个测试方法初始化 DB
- java - 如何使用枚举的getter?
- typescript - 收到警告消息“入口点'@nicky-lenaers/ngx-scroll-to'中的警告包含对'rxjs/index'的深度导入”