部署到 Kubernetes 后无法读取资源文件

我在传统的 tomcat 服务器中运行本地代码,而在部署到 Kubernetes 时,我的读取资源文件代码不起作用。


我放在nas.txt资源文件夹中。



File file = ResourceUtils.getFile("classpath:nas.txt");


//Read File Content

String content = new String(Files.readAllBytes(file.toPath()));

return content;

这是给出"Internal exception has occurred"错误


慕田峪7331174
浏览 273回答 2
2回答

森林海

文件是否存在于tomcat中-->webapps-->app°war-->容器内的资源

慕斯王

ResourceUtils.getFile不适用于打包的罐子。虽然它适用于 IDE。您可以尝试以下替代protected InputStreamReader readStream(String filePath) throws FileNotFoundException {    InputStreamReader streamReader;    if (filePath.startsWith("classpath:")) {        streamReader = new InputStreamReader(getClass().getResourceAsStream(File.separator + filePath.split("classpath:/*")[1]));    } else {        streamReader = new FileReader(ResourceUtils.getFile(filePath));    }    return streamReader;}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java