SpringBootのresourcesフォルダーにあるファイルを取得します

SpringBootのresourcesフォルダーにあるファイルを取得します

たとえば、header.htmlファイルを取得したい
ここに画像の説明を挿入します

File file = ResourceUtils.getFile("classpath:templates/header.html");
ここに画像の説明を挿入します
完全なコード:

@Controller
public class IndexController {

 
    @GetMapping("/header.html")
    @ResponseBody
    public String header() throws IOException {
        File file = ResourceUtils.getFile("classpath:templates/header.html");

        String result = FileUtils.readFileToString(file);


        System.out.println(result);
        return result;
    }

}

postmanを使用してリクエスト送信をシミュレートします
ここに画像の説明を挿入します

結果:
ここに画像の説明を挿入します

おすすめ

転載: blog.csdn.net/Guesshat/article/details/109514912