读取文件方式更换

develop
PL 4 months ago
parent aeb6b5d17e
commit 54bf566482

@ -33,6 +33,10 @@ import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;
import javax.annotation.Resource; import javax.annotation.Resource;
import java.io.*; 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.time.Instant;
import java.util.*; import java.util.*;
@ -309,18 +313,20 @@ public class ChatMessageServiceImpl implements ChatMessageService {
//验证获取模型数据 异常问题 //验证获取模型数据 异常问题
File file = null; File file = null;
try { 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"); //绝对路径获取文件信息 //File file = new File("src/main/resources/Json/ChatModel.json"); //绝对路径获取文件信息
} catch (Exception e) { } catch (Exception e) {
throw new ServiceException(ErrorCode.OPERATION_ERROR.getCode(), "读取文件" + Arrays.toString(e.getStackTrace())); throw new ServiceException(ErrorCode.OPERATION_ERROR.getCode(), "读取文件" + Arrays.toString(e.getStackTrace()));
} }
try { /*try {
//chatModel = readerMethod(file); //chatModel = readerMethod(file);
chatModels = readerMethod(file); chatModels = readerMethod(file);
} catch (Exception e) { } catch (Exception e) {
throw new ServiceException(ErrorCode.OPERATION_ERROR.getCode(), "读取文件并转换实体类" + Arrays.toString(e.getStackTrace())); throw new ServiceException(ErrorCode.OPERATION_ERROR.getCode(), "读取文件并转换实体类" + Arrays.toString(e.getStackTrace()));
} }*/
//循环查找对应的模型数据 //循环查找对应的模型数据
for (ChatModelDto chatModelTemp : chatModels) { for (ChatModelDto chatModelTemp : chatModels) {
@ -513,4 +519,46 @@ public class ChatMessageServiceImpl implements ChatMessageService {
return chatModelDtos; return chatModelDtos;
} }
/**
*
*
* @param fileName
* @return
*/
public static List<ChatModelDto> 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<ChatModelDto> chatModelDtos = new ArrayList<>();
//转换问题
try {
//转换方式
chatModelDtos = JSON.parseObject(strData2, new TypeReference<List<ChatModelDto>>() {
});
} catch (Exception e) {
throw new ServiceException(ErrorCode.OPERATION_ERROR.getCode(), "类型转换失败");
}
return chatModelDtos;
//return content.toString();
}
} }

Loading…
Cancel
Save