springboot jar包启动 读取resource下的文件
生活随笔
收集整理的這篇文章主要介紹了
springboot jar包启动 读取resource下的文件
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1.第一種方式,通過ClassPathResource加載
ClassPathResource classPathResource = new ClassPathResource("templates/test.xlsx"); InputStream inputStream =classPathResource.getInputStream();2.通過當前線程的上下文類加載器獲取
InputStream inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream("excleTemplate/test.xlsx");3.通過當前類加載(未驗證)
InputStream inputStream = this.getClass().getResourceAsStream("templates/propertyUser.xlsx");項目開發時可用這種方式獲取,因為已編譯好在目錄里能找到,但是如果在jar包中執行會找不到,所以不建議,具體原因筆者還在找,希望通過閱讀spring源碼來了解原因。
File file = ResourceUtils.getFile("classpath:excleTemplate/test.xlsx"); InputStream inputStream = new FileInputStream(file);參考:https://www.jianshu.com/p/7d7e5e4e8ae3
總結
以上是生活随笔為你收集整理的springboot jar包启动 读取resource下的文件的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 量子化学计算机程序,量子化学程序gaus
- 下一篇: C语言双人贪吃蛇游戏瘦身版本