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

您的位置:首頁技術文章
文章詳情頁

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

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

資源加載器

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

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

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

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

@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()該方法,則會收到錯誤消息,因為Spring嘗試訪問文件系統路徑,但無法訪問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中從類路徑加載文件的示例的詳細內容,更多關于spring boot 加載文件的資料請關注好吧啦網其它相關文章!

標簽: Spring
相關文章:
主站蜘蛛池模板: 一级片爱爱 | 亚洲免费大全 | 国产一区二区在线视频 | 免费人成综合在线视频 | 国产美女一级特黄毛片 | 欧美国产成人在线 | 精品性久久 | 不卡一级aaa全黄毛片 | 欧美日韩一区在线观看 | 免费国产午夜高清在线视频 | 中文字幕亚洲欧美日韩不卡 | 免费一级欧美在线观看视频片 | 欧美日韩精品一区二区 | 亚洲综色 | 特别福利视频在线观看 | 欧美一级成人毛片影院 | 国产成人精选免费视频 | 怡红院成人永久免费看 | 爽爽日本在线视频免费 | 手机日韩理论片在线播放 | 成人欧美一区二区三区黑人免费 | 国美女福利视频午夜精品 | 一区二区影视 | 一级特级欧美aaaaa毛片 | 亚洲精品在线播放视频 | 亚洲bt欧美bt国产bt | 国产一级性生活 | 欧美日韩在线观看区一二 | 在线综合亚洲欧美自拍 | 亚洲欧美在线免费观看 | 久久亚洲精品中文字幕二区 | 欧美在线精品一区二区三区 | 九色自拍视频 | 国产在线欧美日韩精品一区二区 | 免费看a级毛片 | 国产在线观看免费人成小说 | 久久精品视频16 | 国产成人精品自拍 | xxxx肥婆性bbbb欧美 | 国产微拍精品福利视频 | 国产三级精品三级国产 |