SpringBoot 获取Jar包中静态资源

Spring Boot打包后的Jar包中的资源可通过以下方法获取

 

 

 

    public static String getTemplateContent(String templateName) {
        try {
            ClassPathResource resource = new ClassPathResource(getTemplatePath(templateName));
            var bs = FileCopyUtils.copyToByteArray(resource.getInputStream());
            return new java.lang.String(bs, StandardCharsets.UTF_8);
        } catch (IOException e) {
            e.printStackTrace();
        }

        return null;
    }

    private static String getTemplatePath(String templateName) {
        return "custom/notice-msg-templates/" + templateName + ".json";
    }

 

posted @ 2022-05-18 11:33  xYzDl  阅读(1055)  评论(1)    收藏  举报