偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

開發(fā)必備!Spring Boot 項目中 resources 文件讀取的九大方案詳解

開發(fā) 前端
不同的資源加載方式有不同的適用場景和底層機制,如果使用不當(dāng),不僅可能導(dǎo)致資源讀取失敗,還可能影響程序的可移植性和擴展性。

在 Spring Boot 項目中,resources 目錄承載著大量的關(guān)鍵資源,如配置文件、模板文件、腳本資源、數(shù)據(jù)文件等。而如何以合適的方式高效、安全地讀取這些資源,往往是開發(fā)者繞不過的關(guān)鍵環(huán)節(jié)。

不同的資源加載方式有不同的適用場景和底層機制,如果使用不當(dāng),不僅可能導(dǎo)致資源讀取失敗,還可能影響程序的可移植性和擴展性。

本文將為你系統(tǒng)性地講解 Spring Boot 中讀取 resources 文件的 9 種主流方式,并在最后附上一套完整的控制器 Demo 示例,集中展示這些方式在實際項目中的統(tǒng)一實現(xiàn),幫助你在開發(fā)中快速定位最適合的資源加載方案。

資源讀取的 9 大方式概覽

我們先逐一列出每種方式的核心思路與適用場景。

1、ClassLoader.getResourceAsStream() —— 通用類加載器讀取方式

說明:以類加載器為起點,查找資源路徑。

InputStream inputStream = getClass().getClassLoader().getResourceAsStream("config/sample.txt");
  • 不依賴 Spring,適用于任意 Java 項目;
  • 路徑從 classpath 根路徑開始,不需要加 /。

2、Class.getResourceAsStream() —— 相對于類路徑加載資源

說明:當(dāng)前類對象的路徑定位方式,適合讀取與類位于同一包下的資源。

InputStream inputStream = getClass().getResourceAsStream("/config/sample.txt");
  • 以 / 開頭則從根路徑定位;
  • 相對路徑時以類的包路徑為基準(zhǔn)。

3、使用 Spring 的 ResourceLoader

說明:借助 Spring 提供的通用資源加載抽象,可讀取 classpath、file、http 等協(xié)議資源。

@Resource
private ResourceLoader resourceLoader;

4、使用 ResourceUtils.getFile()

說明:用于將 classpath 路徑資源轉(zhuǎn)為 File 對象,適合需要文件路徑的場景。

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

5、使用 ApplicationContext.getResource()

說明:通過上下文注入加載資源,與 ResourceLoader 類似。

@Resource
private ApplicationContext context;

6、使用 ServletContext.getResourceAsStream()

說明:用于傳統(tǒng) Servlet 模型,從 Web 路徑中獲取資源文件。

@Resource
private ServletContext servletContext;

7、使用 Java IO 的 File

說明:適用于讀取項目中的真實文件,路徑為實際操作系統(tǒng)路徑。

File file = new File("src/main/resources/config/sample.txt");

8、使用 Java NIO 的 Paths 和 Files

說明:使用 Java 8 的現(xiàn)代化文件操作接口,線程安全且效率更高。

Path path = Paths.get("src/main/resources/config/sample.txt");

9、使用 Spring 的 ClassPathResource

說明:Spring 原生支持 classpath 路徑加載,簡單快捷。

ClassPathResource resource = new ClassPathResource("config/sample.txt");

統(tǒng)一完整代碼實現(xiàn)示例

我們將在一個 Spring Boot 控制器中統(tǒng)一實現(xiàn)這 9 種方式,以 /resource/read/{method} 接口形式暴露,讓你一目了然。

文件路徑準(zhǔn)備

請在 src/main/resources/config/sample.txt 文件中放置如下測試內(nèi)容:

這是一個用于演示讀取 resources 文件的示例文本。

控制器實現(xiàn):com.icoderoad.resources.controller.ResourceReadController.java

package com.icoderoad.resources.controller;


import jakarta.annotation.Resource;
import jakarta.servlet.ServletContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.stereotype.Controller;
import org.springframework.util.FileCopyUtils;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.*;


import java.io.*;
import java.nio.file.*;
import java.util.HashMap;
import java.util.Map;


@RestController
@RequestMapping("/resource/read")
public class ResourceReadController {


    @Autowired
    private ResourceLoader resourceLoader;


    @Autowired
    private ApplicationContext applicationContext;


    @Autowired
    private ServletContext servletContext;


    private final String filePath = "config/sample.txt";


    @GetMapping("/{method}")
    public Map<String, Object> readFile(@PathVariable String method) {
        Map<String, Object> result = new HashMap<>();
        try {
            String content = switch (method) {
                case "classloader" -> readByClassLoader();
                case "class" -> readByClass();
                case "loader" -> readByResourceLoader();
                case "utils" -> readByResourceUtils();
                case "context" -> readByApplicationContext();
                case "servlet" -> readByServletContext();
                case "file" -> readByFile();
                case "nio" -> readByNio();
                case "classpath" -> readByClassPathResource();
                default -> "Unsupported method!";
            };
            result.put("method", method);
            result.put("content", content);
        } catch (Exception e) {
            result.put("error", e.getMessage());
        }
        return result;
    }


    private String readByClassLoader() throws IOException {
        try (InputStream in = getClass().getClassLoader().getResourceAsStream(filePath)) {
            return streamToString(in);
        }
    }


    private String readByClass() throws IOException {
        try (InputStream in = getClass().getResourceAsStream("/" + filePath)) {
            return streamToString(in);
        }
    }


    private String readByResourceLoader() throws IOException {
        org.springframework.core.io.Resource resource = resourceLoader.getResource("classpath:" + filePath);
        try (InputStream in = resource.getInputStream()) {
            return streamToString(in);
        }
    }


    private String readByResourceUtils() throws IOException {
        File file = ResourceUtils.getFile("classpath:" + filePath);
        try (InputStream in = new FileInputStream(file)) {
            return streamToString(in);
        }
    }


    private String readByApplicationContext() throws IOException {
        org.springframework.core.io.Resource resource = applicationContext.getResource("classpath:" + filePath);
        try (InputStream in = resource.getInputStream()) {
            return streamToString(in);
        }
    }


    private String readByServletContext() throws IOException {
        // 僅適用于傳統(tǒng)部署模式,如 war 包放入 Tomcat 時
        try (InputStream in = servletContext.getResourceAsStream("/WEB-INF/classes/" + filePath)) {
            return streamToString(in);
        }
    }


    private String readByFile() throws IOException {
        File file = new File("src/main/resources/" + filePath);
        try (InputStream in = new FileInputStream(file)) {
            return streamToString(in);
        }
    }


    private String readByNio() throws IOException {
        Path path = Paths.get("src/main/resources/" + filePath);
        try (InputStream in = Files.newInputStream(path)) {
            return streamToString(in);
        }
    }


    private String readByClassPathResource() throws IOException {
        ClassPathResource resource = new ClassPathResource(filePath);
        try (InputStream in = resource.getInputStream()) {
            return streamToString(in);
        }
    }


    private String streamToString(InputStream in) throws IOException {
        return new String(FileCopyUtils.copyToByteArray(in));
    }
}

使用方式說明

請求路徑

對應(yīng)加載方式

/resource/read/classloader

ClassLoader.getResourceAsStream()

/resource/read/class

Class.getResourceAsStream()

/resource/read/loader

ResourceLoader

/resource/read/utils

ResourceUtils.getFile()

/resource/read/context

ApplicationContext.getResource()

/resource/read/servlet

ServletContext.getResourceAsStream()

/resource/read/file

new File() + FileInputStream

/resource/read/nio

Paths + Files

/resource/read/classpath

ClassPathResource

 總結(jié)

在本文中,我們不僅系統(tǒng)講解了 Spring Boot 項目中讀取 resources 目錄下文件的 9 大方式,還構(gòu)建了一個完整的統(tǒng)一控制器 Demo,集中展示它們在實際項目中的使用方式。你可以根據(jù)以下建議進(jìn)行選擇:

  • 推薦方式(通用性 + 簡潔性):

ClassLoader.getResourceAsStream()

ClassPathResource

ResourceLoader

  • 特定場景方式(依賴文件路徑、Web環(huán)境):
  • File / Paths / ServletContext

理解并熟練掌握這些方式,將極大提高你在 Spring Boot 項目中處理靜態(tài)資源的靈活性與穩(wěn)定性,特別是在構(gòu)建插件機制、配置中心、文件服務(wù)等系統(tǒng)中。

責(zé)任編輯:武曉燕 來源: 路條編程
相關(guān)推薦

2024-09-09 05:30:00

數(shù)據(jù)庫Spring

2016-06-27 15:40:20

戴爾

2024-10-18 16:21:49

SpringPOM

2009-07-28 10:36:37

ASP.NET讀取Ex

2024-04-03 15:40:14

WebSocketWeb應(yīng)用Spring

2023-12-21 14:32:51

Python函數(shù)

2009-09-15 15:51:52

2025-06-26 04:00:00

Spring數(shù)據(jù)綁定技術(shù)

2025-06-19 08:20:00

開發(fā)前端閱讀位置記憶

2009-09-15 16:53:50

2009-10-14 12:56:19

2013-03-31 14:10:55

敏捷開發(fā)

2009-07-24 13:01:44

ASP.NET頁面跳轉(zhuǎn)

2016-11-03 10:03:49

云計算容器超融合

2025-02-05 09:06:35

Spring項目目錄

2025-06-13 07:42:13

2021-09-03 10:08:53

JavaScript開發(fā) 代碼

2009-07-02 09:00:11

RHEL5.4 Bet發(fā)布

2019-03-15 09:00:27

AWSAzure云計算

2009-07-23 13:47:46

ASP.NET數(shù)據(jù)緩存
點贊
收藏

51CTO技術(shù)棧公眾號