如何从资源文件夹Java调用文件

时间:2018-12-07 06:53:52

标签: java spring file spring-boot

enter image description here这是我的代码,用于从资源文件夹获取文件,

ClassLoader classLoader = getClass().getClassLoader();
File fXmlFile = new File(classLoader.getResource("XMLDocument/DoctypesConfig.xml").getFile());

它在本地运行良好,生成docker映像并部署到AWS fargate容器中后会引发异常,

"error_message": "/usr/local/bin/file:/usr/local/bin/app.jar!/BOOT-INF/classes!/XMLDocument/DoctypesConfig.xml (No such file or directory)"

我的资源文件夹结构,

enter image description here

1 个答案:

答案 0 :(得分:2)

在路径前面添加一个斜线:

File fXmlFile = new File(classLoader.getResource("/XMLDocument/DoctypesConfig.xml").getFile());