慕田峪7331174
2022-06-04 16:24:19
我在傳統的 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
提交
取消