亚洲免费在线视频-亚洲啊v-久久免费精品视频-国产精品va-看片地址-成人在线视频网

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

在Spring Boot中從類路徑加載文件的示例

瀏覽:112日期:2023-08-09 11:23:24

資源加載器

使用Java,您可以使用當(dāng)前線程的classLoader并嘗試加載文件,但是Spring Framework為您提供了更為優(yōu)雅的解決方案,例如ResourceLoader。

您只需要自動(dòng)連接ResourceLoader,然后調(diào)用getResource(„somePath“)方法即可。

在Spring Boot(WAR)中從資源目錄/類路徑加載文件的示例

在以下示例中,我們從類路徑中加載名為GeoLite2-Country.mmdb的文件作為資源,然后將其作為File對(duì)象檢索。

@Service('geolocationservice') public class GeoLocationServiceImpl implements GeoLocationService { private static final Logger LOGGER = LoggerFactory.getLogger(GeoLocationServiceImpl.class); private static DatabaseReader reader = null; private ResourceLoader resourceLoader; @Autowired public GeoLocationServiceImpl(ResourceLoader resourceLoader) { this.resourceLoader = resourceLoader; } @PostConstruct public void init() { try { LOGGER.info('GeoLocationServiceImpl: Trying to load GeoLite2-Country database...'); Resource resource = resourceLoader.getResource('classpath:GeoLite2-Country.mmdb'); File dbAsFile = resource.getFile(); // Initialize the reader reader = new DatabaseReader .Builder(dbAsFile) .fileMode(Reader.FileMode.MEMORY) .build(); LOGGER.info('GeoLocationServiceImpl: Database was loaded successfully.'); } catch (IOException | NullPointerException e) { LOGGER.error('Database reader cound not be initialized. ', e); } } @PreDestroy public void preDestroy() { if (reader != null) { try { reader.close(); } catch (IOException e) { LOGGER.error('Failed to close the reader.'); } } } }

在Spring Boot(JAR)中從資源目錄/類路徑加載文件的示例

如果您想從Spring Boot JAR中的 classpath加載文件,則必須使用該resource.getInputStream()方法將其作為InputStream檢索。如果嘗試使用resource.getFile()該方法,則會(huì)收到錯(cuò)誤消息,因?yàn)镾pring嘗試訪問文件系統(tǒng)路徑,但無法訪問JAR中的路徑。

@Service('geolocationservice') public class GeoLocationServiceImpl implements GeoLocationService { private static final Logger LOGGER = LoggerFactory.getLogger(GeoLocationServiceImpl.class); private static DatabaseReader reader = null; private ResourceLoader resourceLoader; @Inject public GeoLocationServiceImpl(ResourceLoader resourceLoader) { this.resourceLoader = resourceLoader; } @PostConstruct public void init() { try { LOGGER.info('GeoLocationServiceImpl: Trying to load GeoLite2-Country database...'); Resource resource = resourceLoader.getResource('classpath:GeoLite2-Country.mmdb'); InputStream dbAsStream = resource.getInputStream(); // <-- this is the difference // Initialize the reader reader = new DatabaseReader .Builder(dbAsStream) .fileMode(Reader.FileMode.MEMORY) .build(); LOGGER.info('GeoLocationServiceImpl: Database was loaded successfully.'); } catch (IOException | NullPointerException e) { LOGGER.error('Database reader cound not be initialized. ', e); } } @PreDestroy public void preDestroy() { if (reader != null) { try { reader.close(); } catch (IOException e) { LOGGER.error('Failed to close the reader.'); } } } }

以上就是在Spring Boot中從類路徑加載文件的示例的詳細(xì)內(nèi)容,更多關(guān)于spring boot 加載文件的資料請(qǐng)關(guān)注好吧啦網(wǎng)其它相關(guān)文章!

標(biāo)簽: Spring
相關(guān)文章:
主站蜘蛛池模板: 国厂自拍 | 亚洲国产精品成人精品软件 | 日本亚洲欧美高清专区vr专区 | 日本美女性爱 | 免费一级在线 | 国产精品欧美一区二区在线看 | 91久久国产成人免费观看资源 | 成年午夜性视频免费播放 | 中文国产成人精品久久一 | 久草视频中文 | 久久99亚洲精品久久久久 | www三级免费 | 老外黑人欧美一级毛片 | 日韩精品小视频 | 久久黄色片| 2021最新国产精品一区 | 日韩a级一片 | 我们2018在线完整免费观看 | 国产精品毛片天天看片 | 午夜亚洲精品 | 免费乱淫视频 | 欧美日韩一区二区视频图片 | 亚洲精品国产综合99久久一区 | 亚洲男人的性天堂 | 免费看一片 | 啪啪自拍 | a站在线观看 | 亚洲精品无码不卡 | 国产原创在线视频 | 亚洲va中文字幕欧美不卡 | 在线看片欧美 | 午夜爱爱毛片xxxx视频免费看 | 性欧美一级毛片 | 亚洲精品区一区二区三区四 | 亚洲一区二区免费视频 | 成人www | 美国三级大片 | 最新精品在线视频 | 成人国产在线24小时播放视频 | 国产一区二区三区在线观看免费 | 成人影院午夜久久影院 |