From 54bf566482176e143f1632f96697a906a19e8e4e Mon Sep 17 00:00:00 2001 From: PL <774412461@qq.com> Date: Fri, 30 Aug 2024 15:56:26 +0800 Subject: [PATCH] =?UTF-8?q?=E8=AF=BB=E5=8F=96=E6=96=87=E4=BB=B6=E6=96=B9?= =?UTF-8?q?=E5=BC=8F=E6=9B=B4=E6=8D=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../service/impl/ChatMessageServiceImpl.java | 56 +++++++++++++++++-- 1 file changed, 52 insertions(+), 4 deletions(-) diff --git a/src/main/java/cn/teammodel/service/impl/ChatMessageServiceImpl.java b/src/main/java/cn/teammodel/service/impl/ChatMessageServiceImpl.java index fbb5a98..ab66df3 100644 --- a/src/main/java/cn/teammodel/service/impl/ChatMessageServiceImpl.java +++ b/src/main/java/cn/teammodel/service/impl/ChatMessageServiceImpl.java @@ -33,6 +33,10 @@ import org.springframework.web.servlet.mvc.method.annotation.SseEmitter; import javax.annotation.Resource; import java.io.*; +import java.net.URISyntaxException; +import java.net.URL; +import java.nio.file.Files; +import java.nio.file.Paths; import java.time.Instant; import java.util.*; @@ -309,18 +313,20 @@ public class ChatMessageServiceImpl implements ChatMessageService { //验证获取模型数据 异常问题 File file = null; try { - file = new File(ClassLoader.getSystemResource("Json/ChatModel.json").getPath()); //相对路径获取文件信息 - + chatModels = readResourceFile("Json/ChatModel.json"); + //String str = readResourceFile("Json/ChatModel.json"); + //file = new File(ClassLoader.getSystemResource("Json/ChatModel.json").getPath()); //相对路径获取文件信息 + //File file = new File("src/main/resources/Json/ChatModel.json"); //绝对路径获取文件信息 } catch (Exception e) { throw new ServiceException(ErrorCode.OPERATION_ERROR.getCode(), "读取文件" + Arrays.toString(e.getStackTrace())); } - try { + /*try { //chatModel = readerMethod(file); chatModels = readerMethod(file); } catch (Exception e) { throw new ServiceException(ErrorCode.OPERATION_ERROR.getCode(), "读取文件并转换实体类" + Arrays.toString(e.getStackTrace())); - } + }*/ //循环查找对应的模型数据 for (ChatModelDto chatModelTemp : chatModels) { @@ -513,4 +519,46 @@ public class ChatMessageServiceImpl implements ChatMessageService { return chatModelDtos; } + + /** + * 读取资源文件 并转换实体类 + * 读取文件信息并转换为智能对话模型数组对象 + * @param fileName + * @return + */ + public static List readResourceFile(String fileName) { + StringBuilder content = new StringBuilder(); + URL resourceUrl = ChatMessageServiceImpl.class.getClassLoader().getResource(fileName); + + if (resourceUrl != null) { + try { + String filePath = Paths.get(resourceUrl.toURI()).toString(); + content.append(new String(Files.readAllBytes(Paths.get(filePath)))); + } catch (IOException | IllegalArgumentException e) { + e.printStackTrace(); + } catch (URISyntaxException e) { + throw new RuntimeException(e); + } + } else { + System.out.println("Resource not found."); + } + + String strData = JSON.toJSONString(content); + //获取聊天字段中的数据 + Object str = JSON.parseObject(strData).get("chatModel"); + String strData2 = JSON.toJSONString(str); + List chatModelDtos = new ArrayList<>(); + + //转换问题 + try { + //转换方式 + chatModelDtos = JSON.parseObject(strData2, new TypeReference>() { + }); + } catch (Exception e) { + throw new ServiceException(ErrorCode.OPERATION_ERROR.getCode(), "类型转换失败"); + } + + return chatModelDtos; + //return content.toString(); + } }