在Spring Boot中从类路径加载文件的示例
作者:程序汪布丁 发布时间:2023-11-11 21:53:44
标签:spring,boot,加载,文件,类路径
资源加载器
使用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
0
投稿
猜你喜欢
- springboot读取文件,打成jar包后访问不到最新开发出现一种情况,springboot打成jar包后读取不到文件,原因是打包之后,文
- 简介对于一个APP来说,肯定会有一个AppBar,这个AppBar一般包含了APP的导航信息等。虽然我们可以用一个固定的组件来做为AppBa
- Bitmap (android.graphics.Bitmap)Bitmap是Android系统中的图像处理的最重要类之一。用它可以获取图像
- package TOOLS;import java.io.BufferedReader;import java.io.File;import
- 双保险线程,每次启动2个相同的线程,互相检测,避免线程死锁造成影响。两个线程都运行,但只有一个线程执行业务,但都会检测对方的时间戳 如果时间
- Java中List.of()和Arrays.asList()的区别及原因动手写一下,让自己更有印象1.Arrays.asList()可以插入
- /** * 进行BigDecimal对象的加减乘除,四舍五入等运算的工具类 * * @author Marydon * @createTi
- Feign的作用是将Http请求抽象化为一个Interface客户端,可以调用接口的形式来执行Http请求,以达到简化Http调用的目的。F
- 问题描述 在某一天打开电脑后,idea里的代码无缘无故地就爆红了,不但spring框架爆红,就
- Android 消息机制1.概述Android应用启动时,会默认有一个主线程(UI线程),在这个线程中会关联一个消息队列(MessageQu
- 线程中断机制提供了一种方法,用于将线程从阻塞等待中唤醒,尝试打断目标线程的现有处理流程,使之响应新的命令。Java 留给开发者这一自由,我们
- 在Android中,线程内部或者线程之间进行信息交互时经常会使用消息,这些基础的东西如果我们熟悉其内部的原理,将会使我们容易、更好地架构系统
- 以前使用MyEclipse已经习惯了,后来改成Eclipse感觉怪怪的。在创建web项目之前首先配置好jdk环境和tomcat环境(即在开发
- 各位亲们可以尝试以下代码:注:这里我就只有一个html标签对来说明问题了,首部之类的东西,自己添加。<html> &n
- 本文实例讲述了C++实现的O(n)复杂度内查找第K大数算法。分享给大家供大家参考,具体如下:题目:是在一组数组(数组元素为整数,可正可负可为
- 自定义 webflux 容器配置配置代码@Componentpublic class ContainerConfig extends Rea
- 概述JavaScript是目前web开发中不可缺少的脚本语言,js不需要编译即可运行,运行在客户端,需要通过浏览器来解析执行JavaScri
- 消息都是存放在一个消息队列中去,而消息循环线程就是围绕这个消息队列进入一个无限循环的,直到线程退出。如果队列中有消息,消息循环线程就会把它取
- 前文本章是关于Java流程控制语句的最全汇总,本篇为汇总上篇。流程是人们生活中不可或缺的一部分,它表示人们每天都在按照一定的流程做事。比如出
- 1.设置url-pattern为*.do(最为常见的方式)只要你的请求url中包含配置的url-pattern,该url就可以到达Dispa