3 回答

TA貢獻(xiàn)1111條經(jīng)驗(yàn) 獲得超0個(gè)贊
因?yàn)楫?dāng)打包的 uber-jar 中不存在您的資源時(shí),類路徑有問題。使用這樣的解決方案
String fuu = "";
ClassPathResource classPathResource = new ClassPathResource("static/foo.txt");
try {
byte[] binaryData = FileCopyUtils.copyToByteArray(classPathResource.getInputStream());
fuu = new String(binaryData, StandardCharsets.UTF_8);
} catch (IOException e) {
e.printStackTrace();
}

TA貢獻(xiàn)1829條經(jīng)驗(yàn) 獲得超13個(gè)贊
試試這個(gè)
IOUtils.toString(new InputStreamReader(this.getClassLoader().getResourceAsStream("fileName.json")))
OR
new InputStreamReader(new ClassPathResource("fileName.json", this.getClassLoader()).getInputStream())
不要使用FileReader或File
使用InputStreamReader , InputStream等

TA貢獻(xiàn)1856條經(jīng)驗(yàn) 獲得超11個(gè)贊
似乎應(yīng)用程序正在嘗試通過 AbstractFileResolvingResource.getFile()(堆棧跟蹤中的幾行)訪問文件,這在可運(yùn)行的 Spring Boot jar 中是不可能的(從 IDE 運(yùn)行時(shí)它可能工作)。
嘗試使用 getInputStream() 代替,例如參見這篇文章。
添加回答
舉報(bào)