在Spring Boot中从类路径加载文件

创建Spring Boot Web应用程序时,有时有时需要从类路径中加载文件;war和jar的加载文件格式是不一样的 2020-09-30 08:26:33 Spring Boot 如何写好单元测试? 单元测试的好处到底有哪些?每次单测启动应用,太耗时,怎么办?二方三方接口可能存在日常没法用,只能上预发/正式的情况,上预发测低效如何处理?本文分享三个单元测试神器及相关经验总结。 2020-09-30 08:08:15 单元测试应用 如何优化尾调用 经常看到关于尾递归这三个词,递归很多时候,都离不开我们,废话不多说,这次我们梳理一遍关于递归那些事。 2020-09-30 08:07:46 如何优化尾调用 JavaScript 中 10 个需要掌握基础的问题 JavaScript 是一种客户端编程语言。全球超过**90%**的网站都在使用它,它是世界上最常用的编程语言之一。因此,今天我们业讨论 10 个有关 JavaScript 的常见问题。 2020-09-30 08:06:39 JavaScript基础编程 浅谈Kotlin的Checked Exception机制 这门语言从一开始的无人问津,到后来成为Android开发的一级语言,再到后来Google官宣的Kotlin First。Kotlin正在被越来越多的开发者接受和认可。 2020-09-30 06:47:22 Kotlin机制 程序员直呼瑞思拜!这7个命令行工具你还没尝试过吗? 终端/命令行是可能是程序员最常用的工具,本文将分享一些我每天都会大量使用的CLI(命令行界面)工具。

创建Spring Boot Web应用程序时,有时有时需要从类路径中加载文件;war和jar的加载文件格式是不一样的

[[344641]]

在下面,您将找到在WAR和JAR中加载文件的解决方案。

资源加载器
使用Java,您可以使用当前线程的classLoader并尝试加载文件,但是Spring Framework为您提供了更为优雅的解决方案,例如ResourceLoader。

您只需要自动连接ResourceLoader,然后调用getResource(„somePath“)方法即可。

在Spring Boot(WAR)中从资源目录/类路径加载文件的示例
在以下示例中,我们从类路径中加载名为GeoLite2-Country.mmdb的文件作为资源,然后将其作为File对象检索。

  1. @Service("geolocationservice")
  2. publicclassGeoLocationServiceImplimplementsGeoLocationService{
  3. privatestaticfinalLoggerLOGGER=LoggerFactory.getLogger(GeoLocationServiceImpl.class);
  4. privatestaticDatabaseReaderreader=null;
  5. privateResourceLoaderresourceLoader;
  6. @Autowired
  7. publicGeoLocationServiceImpl(ResourceLoaderresourceLoader){
  8. this.resourceLoader=resourceLoader;
  9. }@PostConstruct
  10. publicvoidinit(){
  11. try{
  12. LOGGER.info("GeoLocationServiceImpl:TryingtoloadGeoLite2-Countrydatabase...");
  13. Resourceresource=resourceLoader.getResource("classpath:GeoLite2-Country.mmdb");
  14. FiledbAsFile=resource.getFile();//Initializethereader
  15. reader=newDatabaseReader
  16. .Builder(dbAsFile)
  17. .fileMode(Reader.FileMode.MEMORY)
  18. .build();
  19. LOGGER.info("GeoLocationServiceImpl:Databasewasloadedsuccessfully.");
  20. }catch(IOException|NullPointerExceptione){
  21. LOGGER.error("Databasereadercoundnotbeinitialized.",e);
  22. }
  23. }
  24. @PreDestroy
  25. publicvoidpreDestroy(){
  26. if(reader!=null){
  27. try{
  28. reader.close();
  29. }catch(IOExceptione){
  30. LOGGER.error("Failedtoclosethereader.");
  31. }
  32. }
  33. }
  34. }

在Spring Boot(JAR)中从资源目录/类路径加载文件的示例
如果您想从Spring Boot JAR中的 classpath加载文件,则必须使用该resource.getInputStream()方法将其作为InputStream检索。如果尝试使用resource.getFile()该方法,则会收到错误消息,因为Spring尝试访问文件系统路径,但无法访问JAR中的路径。

  1. @Service("geolocationservice")
  2. publicclassGeoLocationServiceImplimplementsGeoLocationService{
  3. privatestaticfinalLoggerLOGGER=LoggerFactory.getLogger(GeoLocationServiceImpl.class);
  4. privatestaticDatabaseReaderreader=null;
  5. privateResourceLoaderresourceLoader;
  6. @Inject
  7. publicGeoLocationServiceImpl(ResourceLoaderresourceLoader){
  8. this.resourceLoader=resourceLoader;
  9. }@PostConstruct
  10. publicvoidinit(){
  11. try{
  12. LOGGER.info("GeoLocationServiceImpl:TryingtoloadGeoLite2-Countrydatabase...");
  13. Resourceresource=resourceLoader.getResource("classpath:GeoLite2-Country.mmdb");
  14. InputStreamdbAsStream=resource.getInputStream();//<--thisisthedifference
  15. //Initializethereader
  16. reader=newDatabaseReader
  17. .Builder(dbAsStream)
  18. .fileMode(Reader.FileMode.MEMORY)
  19. .build();
  20. LOGGER.info("GeoLocationServiceImpl:Databasewasloadedsuccessfully.");
  21. }catch(IOException|NullPointerExceptione){
  22. LOGGER.error("Databasereadercoundnotbeinitialized.",e);
  23. }
  24. }
  25. @PreDestroy
  26. publicvoidpreDestroy(){
  27. if(reader!=null){
  28. try{
  29. reader.close();
  30. }catch(IOExceptione){
  31. LOGGER.error("Failedtoclosethereader.");
  32. }
  33. }
  34. }
  35. }

©本文为清一色官方代发,观点仅代表作者本人,与清一色无关。清一色对文中陈述、观点判断保持中立,不对所包含内容的准确性、可靠性或完整性提供任何明示或暗示的保证。本文不作为投资理财建议,请读者仅作参考,并请自行承担全部责任。文中部分文字/图片/视频/音频等来源于网络,如侵犯到著作权人的权利,请与我们联系(微信/QQ:1074760229)。转载请注明出处:清一色财经

(0)
打赏 微信扫码打赏 微信扫码打赏 支付宝扫码打赏 支付宝扫码打赏
清一色的头像清一色管理团队
上一篇 2023年5月5日 13:37
下一篇 2023年5月5日 13:37

相关推荐

发表评论

登录后才能评论

联系我们

在线咨询:1643011589-QQbutton

手机:13798586780

QQ/微信:1074760229

QQ群:551893940

工作时间:工作日9:00-18:00,节假日休息

关注微信