From 0cdfd1d114f44f6812dc302cde9257068b06a39d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=89=91=E9=9B=A8?= <2375294554@qq.com> Date: Mon, 19 May 2025 10:41:02 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=BB=BA=E8=B5=84=E6=BA=90=E8=AF=BB?= =?UTF-8?q?=E5=8F=96=E5=B7=A5=E5=85=B7=E7=B1=BB=20--ResourcesUtils.java=20?= =?UTF-8?q?=E8=AF=BB=E5=8F=96=E8=B5=84=E6=BA=90=E8=8E=B7=E5=8F=96=E5=88=B0?= =?UTF-8?q?=E5=AD=97=E7=AC=A6=E4=B8=B2=E7=9A=84=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../xiaozhi/common/utils/ResourcesUtils.java | 44 +++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 main/manager-api/src/main/java/xiaozhi/common/utils/ResourcesUtils.java diff --git a/main/manager-api/src/main/java/xiaozhi/common/utils/ResourcesUtils.java b/main/manager-api/src/main/java/xiaozhi/common/utils/ResourcesUtils.java new file mode 100644 index 00000000..49d9a727 --- /dev/null +++ b/main/manager-api/src/main/java/xiaozhi/common/utils/ResourcesUtils.java @@ -0,0 +1,44 @@ +package xiaozhi.common.utils; + +import lombok.AllArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.core.io.ResourceLoader; +import org.springframework.core.io.Resource; +import org.springframework.stereotype.Component; +import xiaozhi.common.exception.RenException; + + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; + +/** + * 资源处理工具 + */ +@AllArgsConstructor +@Slf4j +@Component +public class ResourcesUtils { + private ResourceLoader resourceLoader; + + /** + * 读取资源,返回字符串 + * @param fileName 资源路径:resources下开始 + * @return 字符串 + */ + public String loadString(String fileName) { + Resource resource = resourceLoader.getResource("classpath:" + fileName); + StringBuilder luaScriptBuilder = new StringBuilder(); + try (BufferedReader reader = new BufferedReader( + new InputStreamReader(resource.getInputStream()))) { + String line; + while ((line = reader.readLine()) != null) { + luaScriptBuilder.append(line).append("\n"); + } + } catch (IOException e){ + log.error("方法:loadString()读取资源失败--{}",e.getMessage()); + throw new RenException("读取资源失败"); + } + return luaScriptBuilder.toString(); + } +}