广告
返回顶部
首页 > 资讯 > 后端开发 > JAVA >springboot项目读取 resources 目录下的文件的9种方式(总结)
  • 563
分享到

springboot项目读取 resources 目录下的文件的9种方式(总结)

springbootjavaspring 2023-08-18 21:08:51 563人浏览 八月长安
摘要

1: 使用 ClassLoader.getResourceAsStream() 方法   可以使用类加载器来获取资源文件的输入流。该方法接受一个资源文件路径参数,返回一个 InputStream 对象。 InputStream inputS

1: 使用 ClassLoader.getResourceAsStream() 方法

  可以使用类加载器来获取资源文件的输入流。该方法接受一个资源文件路径参数,返回一个 InputStream 对象。

InputStream inputStream = getClass().getClassLoader().getResourceAsStream("file.txt");

  注意,该方法返回的资源文件路径是相对于类加载器的根路径。因此,对于 resources 目录下的文件,需要在文件名前加上 “classpath:” 前缀。例如: “classpath:file.txt”。

2: 使用 Class.getResourceAsStream() 方法

  可以使用 Class 类的 getResourceAsStream() 方法来读取资源文件。该方法接受一个资源文件路径参数,返回一个 InputStream 对象。

InputStream inputStream = getClass().getResourceAsStream("/file.txt");

  该方法返回的资源文件路径是相对于当前类的路径。因此,对于 resources 目录下的文件,需要在文件名前加上 “/” 前缀。例如: “/file.txt”。

3: 使用 ResourceLoader 加载文件

  可以使用 spring 的 ResourceLoader 接口来加载资源文件。ResourceLoader 接口有一个 getResource() 方法,接受一个资源文件路径参数,返回一个 Resource 对象。

Resource resource = resourceLoader.getResource("classpath:file.txt");InputStream inputStream = resource.getInputStream();

需要注意的是:需要在类中注入 ResourceLoader 对象,并在方法中使用。例如:

@Autowiredprivate ResourceLoader resourceLoader;public void readResourceFile() throws ioException {    Resource resource = resourceLoader.getResource("classpath:file.txt");    InputStream inputStream = resource.getInputStream();}

4: 使用 ResourceUtils 加载文件

  ResourceUtils 是 Spring 提供的一个工具类,用于加载资源文件。可以使用 ResourceUtils.getFile() 方法来获取文件对象。

File file = ResourceUtils.getFile("classpath:file.txt");

需要注意的是:该方法只适用于本地文件系统和 jar 文件。对于 WAR 文件或者其他类型的文件,该方法可能无法正常工作。

5: 使用 ApplicationContext 加载文件

  可以使用 ApplicationContext 的 getResource() 方法来加载资源文件。该方法接受一个资源文件路径参数,返回一个 Resource 对象。

Resource resource = applicationContext.getResource("classpath:file.txt");InputStream inputStream = resource.getInputStream();

需要注意的是:需要在类中注入 ApplicationContext 对象,并在方法中使用。例如:

@Autowiredprivate ApplicationContext applicationContext;public void readResourceFile() throws IOException {    Resource resource = applicationContext.getResource("classpath:file.txt");    InputStream inputStream = resource.getInputStream();}

6: 使用 ServletContext 加载文件

  可以使用 ServletContext 的 getResourceAsStream() 方法来读取资源文件。该方法接受一个资源文件路径参数,返回一个 InputStream 对象。

InputStream inputStream = servletContext.getResourceAsStream("/WEB-INF/classes/file.txt");

需要注意的是:需要在类中注入 ServletContext 对象,并在方法中使用。例如:

@Autowiredprivate ServletContext servletContext;public void readResourceFile() throws IOException {    InputStream inputStream = servletContext.getResourceAsStream("/WEB-INF/classes/file.txt");}

7: 使用 File System 加载文件

  可以使用 File 类来读取资源文件。需要提供完整的文件路径。

File file = new File("src/main/resources/file.txt");InputStream inputStream = new FileInputStream(file);

需要注意的是:使用该方法需要提供完整的文件路径,因此需要知道文件所在的绝对路径。

8: 使用 Paths 和 Files 加载文件

  可以使用 Java NIO 中的 Paths 和 Files 类来读取资源文件。该方法需要提供完整的文件路径。

Path path = Paths.get("src/main/resources/file.txt");InputStream inputStream = Files.newInputStream(path);

  需要注意的是,使用该方法需要提供完整的文件路径,因此需要知道文件所在的绝对路径。

9: 使用 ClassPathResource 加载文件

  可以使用 Spring 提供的 ClassPathResource 类来读取资源文件。该方法需要提供资源文件的相对路径。

ClassPathResource resource = new ClassPathResource("file.txt");InputStream inputStream = resource.getInputStream();

需要注意的是:ClassPathResource 会在类路径下查找资源文件,因此不需要提供完整的文件路径。

10:总结

  以上 9 种方式,都可以用来读取 Spring Boot 项目中 resources 目录下的文件。不同的方法适用于不同的场景,可以根据需要选择合适的方法。

实际开发中推荐使用以下几种方法

  • 使用 ClassLoader.getResourceAsStream() 方法
    这是一种通用的方式,可以适用于大多数情况。

  • 使用 ResourceLoader 加载文件
    如果使用 Spring 框架,可以使用 ResourceLoader 接口来加载资源文件。这种方式适用于大多数 Spring Boot 项目。

  • 使用 ClassPathResource 加载文件
    如果只需要读取 resources 目录下的文件,可以使用 Spring 提供的 ClassPathResource 类来加载文件。这种方式比较简单,不需要提供完整的文件路径。

    需要注意的是:使用不同的方式需要了解其适用的场景和使用方法。对于不同的项目和需求,可能需要选择不同的方式。

来源地址:https://blog.csdn.net/weixin_43025151/article/details/129819888

--结束END--

本文标题: springboot项目读取 resources 目录下的文件的9种方式(总结)

本文链接: https://www.lsjlt.com/news/374793.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作