我在传统的 tomcat 服务器中运行本地代码,而在部署到 Kubernetes 时,我的读取资源文件代码不起作用。我放在nas.txt资源文件夹中。和File file = ResourceUtils.getFile("classpath:nas.txt");//Read File ContentString content = new String(Files.readAllBytes(file.toPath()));return content;这是给出"Internal exception has occurred"错误
2 回答

慕斯王
TA贡献1864条经验 获得超2个赞
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;
}
添加回答
举报
0/150
提交
取消