获取 /resources 目录资源文件的 9 种方法,还有谁不会?!-1

简介: 获取 /resources 目录资源文件的 9 种方法,还有谁不会?!

项目开发中,经常会有一些静态资源,被放置在resources目录下,随项目打包在一起,代码中要使用的时候,通过文件读取的方式,加载并使用;

本文中汇总整理了九种方式获取resources目录下文件的方法。

其中公用的打印文件方法如下:

/**
 * 根据文件路径读取文件内容
 *
 * @param fileInPath
 * @throws IOException
 */
public static void getFileContent(Object fileInPath) throws IOException {
    BufferedReader br = null;
    if (fileInPath == null) {
        return;
    }
    if (fileInPath instanceof String) {
        br = new BufferedReader(new FileReader(new File((String) fileInPath)));
    } else if (fileInPath instanceof InputStream) {
        br = new BufferedReader(new InputStreamReader((InputStream) fileInPath));
    }
    String line;
    while ((line = br.readLine()) != null) {
        System.out.println(line);
    }
    br.close();
}

推荐一个开源免费的 Spring Boot 最全教程:


https://github.com/javastacks/spring-boot-best-practice


方式一

主要核心方法是使用getResource和getPath方法,这里的getResource("")里面是空字符串

public void function1(String fileName) throws IOException {
    String path = this.getClass().getClassLoader().getResource("").getPath();//注意getResource("")里面是空字符串
    System.out.println(path);
    String filePath = path + fileName;
    System.out.println(filePath);
    getFileContent(filePath);
}

方式二

主要核心方法是使用getResourcegetPath方法,直接通过getResource(fileName)方法获取文件路径,注意如果是路径中带有中文一定要使用URLDecoder.decode解码。

/**
 * 直接通过文件名getPath来获取路径
 *
 * @param fileName
 * @throws IOException
 */
public void function2(String fileName) throws IOException {
    String path = this.getClass().getClassLoader().getResource(fileName).getPath();//注意getResource("")里面是空字符串
    System.out.println(path);
    String filePath = URLDecoder.decode(path, "UTF-8");//如果路径中带有中文会被URLEncoder,因此这里需要解码
    System.out.println(filePath);
    getFileContent(filePath);
}

方式三

直接通过文件名+getFile()来获取文件。如果是文件路径的话getFilegetPath效果是一样的,如果是URL路径的话getPath是带有参数的路径。

如下所示:

url.getFile()=/pub/files/foobar.txt?id=123456
url.getPath()=/pub/files/foobar.txt

使用getFile()方式获取文件的代码如下:

/**
 * 直接通过文件名+getFile()来获取
 *
 * @param fileName
 * @throws IOException
 */
public void function3(String fileName) throws IOException {
    String path = this.getClass().getClassLoader().getResource(fileName).getFile();//注意getResource("")里面是空字符串
    System.out.println(path);
    String filePath = URLDecoder.decode(path, "UTF-8");//如果路径中带有中文会被URLEncoder,因此这里需要解码
    System.out.println(filePath);
    getFileContent(filePath);
}

方式四(重要)

直接使用getResourceAsStream方法获取流,上面的几种方式都需要获取文件路径,但是在SpringBoot中所有文件都在jar包中,没有一个实际的路径,因此可以使用以下方式。

/**
 * 直接使用getResourceAsStream方法获取流
 * springboot项目中需要使用此种方法,因为jar包中没有一个实际的路径存放文件
 *
 * @param fileName
 * @throws IOException
 */
public void function4(String fileName) throws IOException {
    InputStream in = this.getClass().getClassLoader().getResourceAsStream(fileName);
    getFileContent(in);
}
相关文章
|
Java 容器
SpringBoot读取resources下的文件以及resources的资源路径
SpringBoot读取resources下的文件以及resources的资源路径
758 0
|
移动开发 Java HTML5
Springboot web静态资源配置
Springboot web静态资源配置
1457 0
|
安全 Java API
深入解析 Spring Security 配置中的 CSRF 启用与 requestMatchers 报错问题
本文深入解析了Spring Security配置中CSRF启用与`requestMatchers`报错的常见问题。针对CSRF,指出默认已启用,无需调用`enable()`,只需移除`disable()`即可恢复。对于`requestMatchers`多路径匹配报错,分析了Spring Security 6.x中方法签名的变化,并提供了三种解决方案:分次调用、自定义匹配器及降级使用`antMatchers()`。最后提醒开发者关注版本兼容性,确保升级平稳过渡。
1560 2
|
12月前
|
人工智能 Java 数据库
如何保证接口幂等性?
在分布式系统中,接口幂等性至关重要。本文详解其定义、重要性及实现方案,包括唯一索引、Token机制、分布式锁、状态机与版本号机制,并提供最佳实践建议,助你提升系统可靠性与用户体验。
2358 1
|
程序员
【工具使用】Intellij IDEA 自动清除无效 import 包 和 清除无效 import包 的快捷键
【工具使用】Intellij IDEA 自动清除无效 import 包 和 清除无效 import包 的快捷键
6003 0
|
负载均衡 IDE Java
SpringBoot整合XXL-JOB【04】- 以GLUE模式运行与执行器负载均衡策略
在本节中,我们将介绍XXL-JOB的GLUE模式和集群模式下的路由策略。GLUE模式允许直接在线上改造方法为定时任务,无需重新部署。通过一个测试方法,展示了如何在调度中心配置并使用GLUE模式执行定时任务。接着,我们探讨了多实例环境下的负载均衡策略,确保任务不会重复执行,并可通过修改路由策略(如轮训)实现任务在多个实例间的均衡分配。最后,总结了GLUE模式和负载均衡策略的应用,帮助读者更深入理解XXL-JOB的使用。
1140 9
SpringBoot整合XXL-JOB【04】-  以GLUE模式运行与执行器负载均衡策略
|
IDE Java 数据库连接
SpringBoot整合XXL-JOB【02】- 启动调度中心
本文介绍了如何初始化和配置XXL-JOB调度中心。首先,从GitHub或Gitee获取源码;接着,执行`tables_xxl_job.sql`脚本初始化数据库。然后,在IDE中打开项目并修改`application.properties`中的数据库连接和`accessToken`配置。完成配置后,启动`XxlJobAdminApplication`,访问http://localhost:8080/xxl-job-admin/进行登录。最后,简要介绍了调度中心的主要功能模块,包括运行报表、任务管理、调度日志、执行器管理和用户管理。下篇将通过实例演示如何使用XXL-JOB执行定时任务。
990 6
SpringBoot整合XXL-JOB【02】- 启动调度中心
|
Java Maven
Maven编译报错:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.13.0:compile 解决方案
在执行Maven项目中的`install`命令时,遇到编译插件版本不匹配的错误。具体报错为:`maven-compiler-plugin:3.13.0`要求Maven版本至少为3.6.3。解决方案是将Maven版本升级到3.6.3或降低插件版本。本文详细介绍了如何下载、解压并配置Maven 3.6.3,包括环境变量设置和IDEA中的Maven配置,确保项目顺利编译。
17651 5
Maven编译报错:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.13.0:compile 解决方案

热门文章

最新文章