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

开发
创建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. public class GeoLocationServiceImpl implements GeoLocationService { 
  3.     private static final Logger LOGGER = LoggerFactory.getLogger(GeoLocationServiceImpl.class); 
  4.     private static DatabaseReader reader = null
  5.     private ResourceLoader resourceLoader; 
  6.     @Autowired 
  7.     public GeoLocationServiceImpl(ResourceLoader resourceLoader) { 
  8.         this.resourceLoader = resourceLoader; 
  9.     }    @PostConstruct 
  10.     public void init() { 
  11.         try { 
  12.             LOGGER.info("GeoLocationServiceImpl: Trying to load GeoLite2-Country database..."); 
  13.             Resource resource = resourceLoader.getResource("classpath:GeoLite2-Country.mmdb"); 
  14.             File dbAsFile = resource.getFile();            // Initialize the reader 
  15.             reader = new DatabaseReader 
  16.                         .Builder(dbAsFile) 
  17.                         .fileMode(Reader.FileMode.MEMORY) 
  18.                         .build(); 
  19.             LOGGER.info("GeoLocationServiceImpl: Database was loaded successfully."); 
  20.         } catch (IOException | NullPointerException e) { 
  21.             LOGGER.error("Database reader cound not be initialized. ", e); 
  22.         } 
  23.     } 
  24.     @PreDestroy 
  25.     public void preDestroy() { 
  26.         if (reader != null) { 
  27.             try { 
  28.                 reader.close(); 
  29.             } catch (IOException e) { 
  30.                 LOGGER.error("Failed to close the reader."); 
  31.             } 
  32.         } 
  33.     } 

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

  1. @Service("geolocationservice"
  2. public class GeoLocationServiceImpl implements GeoLocationService { 
  3.     private static final Logger LOGGER = LoggerFactory.getLogger(GeoLocationServiceImpl.class); 
  4.     private static DatabaseReader reader = null
  5.     private ResourceLoader resourceLoader; 
  6.     @Inject 
  7.     public GeoLocationServiceImpl(ResourceLoader resourceLoader) { 
  8.         this.resourceLoader = resourceLoader; 
  9.     }    @PostConstruct 
  10.     public void init() { 
  11.         try { 
  12.             LOGGER.info("GeoLocationServiceImpl: Trying to load GeoLite2-Country database..."); 
  13.             Resource resource = resourceLoader.getResource("classpath:GeoLite2-Country.mmdb"); 
  14.             InputStream dbAsStream = resource.getInputStream(); // <-- this is the difference 
  15.             // Initialize the reader 
  16.             reader = new DatabaseReader 
  17.                         .Builder(dbAsStream) 
  18.                         .fileMode(Reader.FileMode.MEMORY) 
  19.                         .build(); 
  20.             LOGGER.info("GeoLocationServiceImpl: Database was loaded successfully."); 
  21.         } catch (IOException | NullPointerException e) { 
  22.             LOGGER.error("Database reader cound not be initialized. ", e); 
  23.         } 
  24.     } 
  25.     @PreDestroy 
  26.     public void preDestroy() { 
  27.         if (reader != null) { 
  28.             try { 
  29.                 reader.close(); 
  30.             } catch (IOException e) { 
  31.                 LOGGER.error("Failed to close the reader."); 
  32.             } 
  33.         } 
  34.     } 

 

 

责任编辑:姜华 来源: 今日头条
相关推荐

2023-09-22 10:12:57

2019-12-09 15:08:30

JavaTomcatWeb

2024-01-23 08:47:13

BeanSpring加载方式

2022-09-02 08:19:37

spring配置加载

2011-07-07 17:30:25

iPhone Xcode

2019-07-15 10:00:53

DockerJava容器

2021-09-01 10:07:43

开发零搭建Groovy

2021-10-18 12:04:22

Spring BootJava开发

2021-10-18 10:36:31

Spring Boot插件Jar

2019-07-15 16:00:24

Docker架构容器

2022-11-01 08:55:55

编译工具MavenSpring

2020-10-25 16:53:56

Linux 开源操作系统

2014-04-21 10:53:52

Linux 命令行终端下载文件

2020-06-23 09:55:40

Spring Boo指标Java

2023-11-29 07:34:05

Spring工具类项目

2022-07-07 14:18:43

SpringWeb应用设计

2023-12-08 07:52:51

Spring项目开发

2019-08-15 10:56:10

WebServletSpring mvc

2022-09-19 10:48:14

Spring对象项目

2023-11-01 08:30:20

SpringYAML
点赞
收藏

51CTO技术栈公众号