首页 > 其他分享 >SpringBoot读取Resources下的文件

SpringBoot读取Resources下的文件

时间:2024-05-10 14:11:26浏览次数:10  
标签:Resource SpringBoot resourceLoader springframework io org import Resources 读取

package com.qzsl.dp.utils;

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.stereotype.Component;
import org.springframework.util.FileCopyUtils;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;

/** 获取初始化文件
 * @author --
 * @since 2024/5/10
 **/
@Component
public class ResourceReader {


    private final ResourceLoader resourceLoader;

    public ResourceReader(@Qualifier("gridFsTemplate") ResourceLoader resourceLoader) {
        this.resourceLoader = resourceLoader;
    }

    /** 读取resources下的文件
     * @param fileName 如:/q.txt 或者(在sql文件夹下) /sql/text.sql
     * @return {@link String}
     * @since 2024/5/10
     **/
    public String readResourceFile(String fileName) throws IOException {
        // 获取资源文件的 Resource 对象
        Resource resource = resourceLoader.getResource("classpath:" + fileName);
        
        // 检查资源文件是否存在
        if (resource.exists()) {
            // 使用 try-with-resources 自动关闭 Reader
            try (Reader reader = new InputStreamReader(resource.getInputStream())) {
                // 使用 FileCopyUtils 将内容读取到字符串中
                return FileCopyUtils.copyToString(reader);
            }
        } else {
            throw new IOException("Resource file not found: " + fileName);
        }
    }


}

标签:Resource,SpringBoot,resourceLoader,springframework,io,org,import,Resources,读取
From: https://www.cnblogs.com/kakarotto-chen/p/18184192

相关文章

  • springboot JunitTest
    junit测试参考官方文档:https://docs.spring.io/spring-boot/docs/2.0.4.RELEASE/reference/html/boot-features-testing.html1.对springboot框架的项目进行测试,需要引入测试包<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boo......
  • SpringBoot中全部注解归纳解释
    https://blog.csdn.net/weixin_55772633/article/details/131882825https://www.cnblogs.com/jingzh/p/14620211.html1springboot注解1.1引言1.2基本注解1.3JPA注解1.4SpringMVC相关注解1.5全局异常处理1.6项目中具体配置解析和使用环境1.7Lombok注解1.8数......
  • SpringBoot+使用过滤器链执行风控决策
    风控流程下单前进行风控校验//1.begin---风控处理---前置处理{黑白名单校验}RiskControlRuleEnumcontrolRuleEnum=riskControlHandlerService.preHandle(mappingObj.getMerchantGoodsType(),thatUser);if(controlRuleEnum!=null){log......
  • WPF dynamic resources drawbacks
     Dynamic resource, on the other hand, will create a temporary expression during the initial compilation and thus defer lookup for resources until the requested resource value is actually required in order to construct an obj......
  • Springboot项目镜像制作&传递环境变量、设置hostname、动态设置JVM参数、cmd&entrypoi
    实现制作一个springboot的镜像,并且可以传递环境变量实现动态JVM参数和端口。0.准备&cmd、entrypoint区别1.准备springboot项目一个简单的springboot项目,默认启动8001端口,里面只有一个接口。xxx%curllocalhost:8081indexdocker环境2.CMD、entrypoint区......
  • springboot3.2.3如何集成swagger
    在SpringBoot中集成Swagger可以方便地生成API文档并进行接口测试。要在SpringBoot3.2.3中集成Swagger,你可以按照以下步骤进行操作:1.添加Swagger依赖到pom.xml文件中:点击查看代码<dependency><groupId>io.springfox</groupId><artifactId>springfox-boot-starter<......
  • openfeign接口Springboot启动Bean报错未找到Singleton bean creation not allowed whi
    检查步骤检查springboot启动类是否标注@EnableFeignClients注解,未标注该注解会导致无法注入bean检查远程调用模块是否标注注解@FeignClient检查@FeignClient注解中是否写了正确的微服务名称(区分大小写)检查@FeignClient注解中标识的微服务是否启动​​原因:此处接......
  • springboot+vue项目
    1MyBatisPlus的分页插件是怎么生效的?体现在哪里?PaginationInnerInterceptor是通过拦截数据库操作来实现分页功能的。 MyBatisPlus的分页插件PaginationInnerInterceptor是通过拦截数据库操作来实现分页功能的。它的工作原理如下:配置分页插件:在你的SpringBoot应用......
  • SpringBoot随手笔记
    SpringBoot随手笔记0关于火狐浏览器什么时候会发出http请求的说明在抓包的情况下(按下F12后的模式),不管是刷新页面还是在浏览器地址栏回车,该页面中的图片都会发出http请求;但如果不是抓包的模式下,如果访问的页面和上一次访问的页面相同(地址栏的地址没有更改),不管是刷新页面还......
  • springboot seata 全局捕获异常失效
    问题:Springboot使用@ControllerAdvice或@RestControllerAdvice全局捕获异常时,捕获不到自己抛出的相应异常首先看一下全局异常组件有么有被扫描到如何查看,很简单只需要写一段类加载打印代码,如下 如果启动时,打印了你写的字符串就说明时烧苗到了 这就说明是其他的问题了,那就......