在Spring Boot中从类路径加载文件的示例
作者:程序汪布丁 时间:2023-11-11 21:53:44
资源加载器
使用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.");
}
}
}
}
来源:https://developer.51cto.com/art/202009/627661.htm
标签:spring,boot,加载,文件,类路径
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Java压缩/解压文件的实现代码
2023-08-26 04:02:56
android调试工具DDMS的使用详解
2023-06-21 09:06:22
![](https://img.aspxhome.com/file/2023/4/82414_0s.png)
Java实现SSL双向认证的方法
2023-09-22 10:34:35
![](https://img.aspxhome.com/file/2023/0/58090_0s.jpg)
Flutter 实现下拉刷新上拉加载的示例代码
2023-08-18 21:31:16
![](https://img.aspxhome.com/file/2023/8/88668_0s.gif)
Java编程中使用XFire框架调用WebService程序接口
2023-11-06 20:16:33
![](https://img.aspxhome.com/file/2023/0/58990_0s.png)
快速了解hibernate配置文件与映射文件
2023-11-04 23:02:26
springcloud feign传输List的坑及解决
2023-06-20 18:31:57
go打包aar及flutter调用aar流程详解
2023-06-24 17:39:21
![](https://img.aspxhome.com/file/2023/4/87634_0s.png)
详解SpringBoot Start组件开发之记录接口日志信息
2023-07-26 18:33:34
![](https://img.aspxhome.com/file/2023/5/57835_0s.jpg)
java15新功能的详细讲解
2023-08-23 04:40:21
Java在web页面上的编码解码处理及中文URL乱码解决
2023-08-25 11:10:19
![](https://img.aspxhome.com/file/2023/6/58206_0s.gif)
idea启动springmvc项目时报找不到类的解决方法
2023-11-09 16:51:04
![](https://img.aspxhome.com/file/2023/6/59416_0s.png)
线程局部变量的实现 ThreadLocal使用及场景介绍
2023-11-10 03:19:26
![](https://img.aspxhome.com/file/2023/1/59031_0s.jpg)
Kotlin中的惰性操作容器Sequence序列使用原理详解
2023-10-01 14:21:55
![](https://img.aspxhome.com/file/2023/7/84717_0s.jpg)
springboot2.x 接入阿里云市场短信发送的实现
2023-09-20 23:03:57
![](https://img.aspxhome.com/file/2023/3/58593_0s.png)
Zookeeper和Eureka哪个更好?
2023-11-10 02:57:35
Dubbo实现分布式日志链路追踪
2023-08-23 21:00:54
![](https://img.aspxhome.com/file/2023/2/58292_0s.png)
基于sharding-jdbc的使用限制
2023-09-01 12:33:58
Java多线程Atomic包操作原子变量与原子类详解
2023-08-18 07:46:25
android效果TapBarMenu绘制底部导航栏的使用方式示例
2023-07-29 20:53:36
![](https://img.aspxhome.com/file/2023/2/84722_0s.gif)