【新增】 1、答题状态保存与回显;2、邮箱,edge,选择题更换pc答案回显
This commit is contained in:
@@ -5,6 +5,7 @@ import com.example.exam.exam.dal.StuPaperScoreDO;
|
||||
import com.example.exam.exam.service.autoForEmailSetting.AutoForWinEmailSettingService;
|
||||
import com.example.exam.exam.service.autoForWinEdgeSetting.AutoForWinEdgeSettingService;
|
||||
import com.example.exam.exam.service.autoforbrower.AutoForBrowerService;
|
||||
import com.example.exam.exam.service.autoforbutton.AutoForButtonService;
|
||||
import com.example.exam.exam.service.autoforps.AutoForPsService;
|
||||
import com.example.exam.exam.service.brower.JudgementBrowerService;
|
||||
import com.example.exam.exam.service.autoforc.AutoForCService;
|
||||
@@ -38,6 +39,8 @@ public class AutoController {
|
||||
@Resource
|
||||
AutoForChoiceService autoForChoiceService;
|
||||
@Resource
|
||||
AutoForButtonService autoForButtonService;
|
||||
@Resource
|
||||
AutoForMysqlService autoForMysqlService;
|
||||
@Resource
|
||||
AutoForFileService autoForFileService;
|
||||
@@ -257,6 +260,7 @@ public class AutoController {
|
||||
return Result.success(autoForChoiceService.autoForChoiceToJson(stuInfoVo));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 选择题判分
|
||||
*
|
||||
@@ -267,4 +271,16 @@ public class AutoController {
|
||||
public Result<Double> judgementForChoice(@RequestBody StuInfoVo stuInfoVo) throws IOException {
|
||||
return Result.success(autoForChoiceService.autoForChoice(stuInfoVo));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 学生端答题后按钮状态记录
|
||||
*
|
||||
* @param stuInfoVo 学生信息
|
||||
* @return 分数
|
||||
*/
|
||||
@PostMapping("/judgementForButtonToJson")
|
||||
public Result<String> judgementForButtonToJson(@RequestBody StuInfoVo stuInfoVo) {
|
||||
return Result.success(autoForButtonService.autoForButtonToJson(stuInfoVo));
|
||||
}
|
||||
}
|
||||
|
@@ -0,0 +1,77 @@
|
||||
package com.example.exam.exam.controller.info;
|
||||
|
||||
import com.example.exam.exam.controller.auto.vo.StuInfoVo;
|
||||
import com.example.exam.exam.controller.info.vo.EmailInfoVo;
|
||||
import com.example.exam.exam.controller.info.vo.EdgeInfoVo;
|
||||
import com.example.exam.exam.service.autoForEmailSetting.AutoForWinEmailSettingService;
|
||||
import com.example.exam.exam.service.autoForWinEdgeSetting.AutoForWinEdgeSettingService;
|
||||
import com.example.exam.exam.service.autoforbutton.AutoForButtonService;
|
||||
import com.example.exam.exam.service.autoforchoice.AutoForChoiceService;
|
||||
import com.example.exam.exam.utils.Result;
|
||||
import jakarta.annotation.Resource;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @author REN
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("/exam/info")
|
||||
public class InfoController {
|
||||
|
||||
@Resource
|
||||
AutoForWinEdgeSettingService autoForWinEdgeSettingService;
|
||||
@Resource
|
||||
AutoForWinEmailSettingService autoForWinEmailSettingService;
|
||||
@Resource
|
||||
AutoForChoiceService autoForChoiceService;
|
||||
@Resource
|
||||
AutoForButtonService autoForButtonService;
|
||||
|
||||
/**
|
||||
* edge网络设置 获取文件内的答案进行回显设置
|
||||
*
|
||||
* @param stuInfoVo 学生信息
|
||||
* @return String
|
||||
*/
|
||||
@PostMapping("/getEdgeDummyInfo")
|
||||
public Result<List<EdgeInfoVo>> getEdgeDummyInfo(@RequestBody StuInfoVo stuInfoVo) throws IOException {
|
||||
return Result.success(autoForWinEdgeSettingService.getEdgeInfo(stuInfoVo));
|
||||
}
|
||||
|
||||
/**
|
||||
* 邮箱设置 获取文件内的答案进行回显设置
|
||||
*
|
||||
* @param stuInfoVo 学生信息
|
||||
* @return String
|
||||
*/
|
||||
@PostMapping("/getEmailDummyInfo")
|
||||
public Result<List<EmailInfoVo>> getEmailDummyInfo(@RequestBody StuInfoVo stuInfoVo) throws IOException {
|
||||
return Result.success(autoForWinEmailSettingService.getEmailInfo(stuInfoVo));
|
||||
}
|
||||
|
||||
/**
|
||||
* 选择题 获取文件内的答案进行回显设置
|
||||
*
|
||||
* @param stuInfoVo 学生信息
|
||||
* @return String
|
||||
*/
|
||||
@PostMapping("/getChoiceInfo")
|
||||
public Result<Map<String, String>> getChoiceInfo(@RequestBody StuInfoVo stuInfoVo) throws IOException {
|
||||
return Result.success(autoForChoiceService.getChoiceInfo(stuInfoVo));
|
||||
}
|
||||
|
||||
/**
|
||||
* 答题标识 获取文件内进行回显设置
|
||||
*
|
||||
* @param stuInfoVo 学生信息
|
||||
* @return String
|
||||
*/
|
||||
@PostMapping("/getButtonInfo")
|
||||
public Result<Map<String, String>> getButtonInfo(@RequestBody StuInfoVo stuInfoVo) throws IOException {
|
||||
return Result.success(autoForButtonService.getButtonInfo(stuInfoVo));
|
||||
}
|
||||
}
|
@@ -0,0 +1,14 @@
|
||||
package com.example.exam.exam.controller.info.vo;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
@Data
|
||||
public class EdgeInfoVo {
|
||||
// 试题ID
|
||||
private String quId;
|
||||
|
||||
// 答案
|
||||
private Map<String,String> map;
|
||||
}
|
@@ -0,0 +1,14 @@
|
||||
package com.example.exam.exam.controller.info.vo;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
@Data
|
||||
public class EmailInfoVo {
|
||||
// 试题ID
|
||||
private String quId;
|
||||
|
||||
// 答案
|
||||
private Map<String,String> map;
|
||||
}
|
@@ -1,9 +1,11 @@
|
||||
package com.example.exam.exam.service.autoForEmailSetting;
|
||||
|
||||
import com.example.exam.exam.controller.auto.vo.StuInfoVo;
|
||||
import com.example.exam.exam.controller.info.vo.EmailInfoVo;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.math.BigDecimal;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author REN
|
||||
@@ -13,4 +15,6 @@ public interface AutoForWinEmailSettingService {
|
||||
String autoForEmailToJson(StuInfoVo stuInfoVo);
|
||||
|
||||
BigDecimal autoForEmail(StuInfoVo stuInfoVo) throws IOException;
|
||||
|
||||
List<EmailInfoVo> getEmailInfo(StuInfoVo stuInfoVo);
|
||||
}
|
||||
|
@@ -1,6 +1,7 @@
|
||||
package com.example.exam.exam.service.autoForEmailSetting;
|
||||
|
||||
import com.example.exam.exam.controller.auto.vo.StuInfoVo;
|
||||
import com.example.exam.exam.controller.info.vo.EmailInfoVo;
|
||||
import com.example.exam.exam.dal.*;
|
||||
import com.example.exam.exam.mapper.EducationPaperMapper;
|
||||
import com.example.exam.exam.mapper.EducationPaperQuMapper;
|
||||
@@ -298,6 +299,97 @@ public class AutoForWinEmailSettingServiceImpl implements AutoForWinEmailSetting
|
||||
return score.setScale(1, BigDecimal.ROUND_HALF_UP);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public List<EmailInfoVo> getEmailInfo(StuInfoVo stuInfoVo) {
|
||||
List<EmailInfoVo> emailInfoVos = new ArrayList<>();
|
||||
// 获取Paper下的所有windows网络设置ID
|
||||
List<ExamQuestion> examQuestions = educationPaperMapper.selectPaperQuByPaperId(stuInfoVo.getPaperId());
|
||||
List<ExamQuestion> edgeList = new ArrayList<>();
|
||||
for (ExamQuestion examQuestion : examQuestions) {
|
||||
if ("邮箱".equals(examQuestion.getSubjectName())) {
|
||||
edgeList.add(examQuestion);
|
||||
}
|
||||
}
|
||||
// 获取所有json文件,并取出试题ID
|
||||
List<File> resultFile = new ArrayList<>();
|
||||
File dir = new File(stuInfoVo.getFilePath());
|
||||
File[] files = dir.listFiles();
|
||||
if (files == null) return null;
|
||||
// 获取到题型得文件夹
|
||||
for (File file : files) {
|
||||
File[] fileQuNum = file.listFiles();
|
||||
if (fileQuNum != null) {
|
||||
for (File fileNum : fileQuNum) {
|
||||
if (Objects.requireNonNull(fileNum.listFiles()).length > 0) {
|
||||
File fileJson = Objects.requireNonNull(fileNum.listFiles())[0];
|
||||
if (fileJson.isFile()
|
||||
&& fileJson.getName().contains("@EmailDummy")
|
||||
&& fileJson.getName().toLowerCase().endsWith(".json")) {
|
||||
resultFile.add(fileJson);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
List<String> trueFileQuid = new ArrayList<>();
|
||||
List<String> noFileQuid = new ArrayList<>();
|
||||
// 开始读取文件并进行拆分
|
||||
for (ExamQuestion examQuestion : edgeList) {
|
||||
boolean fileIsTrue = false;
|
||||
for (File file : resultFile) {
|
||||
String quId = file.getName().split("@")[0];
|
||||
if (quId.equals(examQuestion.getQuId())) {
|
||||
fileIsTrue = true;
|
||||
trueFileQuid.add(quId);
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!fileIsTrue) {
|
||||
noFileQuid.add(examQuestion.getQuId());
|
||||
}
|
||||
}
|
||||
// 查询哪些文件已经进行获取到学生作答文件了
|
||||
for (String str : trueFileQuid) {
|
||||
// 创建 EmailInfoVo 对象
|
||||
EmailInfoVo emailInfoVo = new EmailInfoVo();
|
||||
// 查询到学生文件
|
||||
Optional<File> fileResult = resultFile.stream().filter(resultFiles -> resultFiles.getName().contains(str)).findFirst();
|
||||
if (fileResult.isPresent()) {
|
||||
File file = fileResult.get();
|
||||
// 创建 ObjectMapper 实例
|
||||
File jsonFile = new File(file.getPath());
|
||||
ObjectMapper objectMapper = new ObjectMapper();
|
||||
if (jsonFile.exists()) {
|
||||
// 试题ID
|
||||
String quId = file.getName().split("@")[0];
|
||||
emailInfoVo.setQuId(quId);
|
||||
// 读取学生文件
|
||||
try (FileInputStream fileInputStream = new FileInputStream(jsonFile)) {
|
||||
// 读取文件并转换为 JsonNode
|
||||
JsonNode rootNode = objectMapper.readTree(fileInputStream);
|
||||
String jsonText = objectMapper.writeValueAsString(rootNode);
|
||||
// 从 JSON 文件读取并转换为 Person 对象
|
||||
Map<String, String> map = objectMapper.readValue(jsonText, Map.class);
|
||||
emailInfoVo.setMap(map);
|
||||
emailInfoVos.add(emailInfoVo);
|
||||
if (fileInputStream != null) {
|
||||
try {
|
||||
fileInputStream.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return emailInfoVos;
|
||||
}
|
||||
|
||||
|
||||
// 将Map写入JSON文件
|
||||
// 将Map追加到JSON文件
|
||||
public static String writeMapToJson(Map<String, String> map, String filePath) {
|
||||
|
@@ -1,6 +1,7 @@
|
||||
package com.example.exam.exam.service.autoForWinEdgeSetting;
|
||||
|
||||
import com.example.exam.exam.controller.auto.vo.StuInfoVo;
|
||||
import com.example.exam.exam.controller.info.vo.EdgeInfoVo;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.math.BigDecimal;
|
||||
@@ -14,4 +15,6 @@ public interface AutoForWinEdgeSettingService {
|
||||
String autoForEdgeToJson(StuInfoVo stuInfoVo);
|
||||
|
||||
BigDecimal autoForEdge(StuInfoVo stuInfoVo) throws IOException;
|
||||
|
||||
List<EdgeInfoVo> getEdgeInfo(StuInfoVo stuInfoVo);
|
||||
}
|
||||
|
@@ -1,6 +1,7 @@
|
||||
package com.example.exam.exam.service.autoForWinEdgeSetting;
|
||||
|
||||
import com.example.exam.exam.controller.auto.vo.StuInfoVo;
|
||||
import com.example.exam.exam.controller.info.vo.EdgeInfoVo;
|
||||
import com.example.exam.exam.dal.*;
|
||||
import com.example.exam.exam.mapper.EducationPaperMapper;
|
||||
import com.example.exam.exam.mapper.EducationPaperQuMapper;
|
||||
@@ -293,6 +294,90 @@ public class AutoForWinEdgeSettingServiceImpl implements AutoForWinEdgeSettingSe
|
||||
return score.setScale(1, BigDecimal.ROUND_HALF_UP);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<EdgeInfoVo> getEdgeInfo(StuInfoVo stuInfoVo) {
|
||||
List<EdgeInfoVo> edgeInfoVos = new ArrayList<>();
|
||||
// 获取Paper下的所有windows网络设置ID
|
||||
List<ExamQuestion> examQuestions = educationPaperMapper.selectPaperQuByPaperId(stuInfoVo.getPaperId());
|
||||
List<ExamQuestion> edgeList = new ArrayList<>();
|
||||
for (ExamQuestion examQuestion : examQuestions) {
|
||||
if ("windows网络设置".equals(examQuestion.getSubjectName())) {
|
||||
edgeList.add(examQuestion);
|
||||
}
|
||||
}
|
||||
// 获取所有json文件,并取出试题ID
|
||||
List<File> resultFile = new ArrayList<>();
|
||||
File dir = new File(stuInfoVo.getFilePath());
|
||||
File[] files = dir.listFiles();
|
||||
if (files == null) return null;
|
||||
// 获取到题型得文件夹
|
||||
for (File file : files) {
|
||||
File[] fileQuNum = file.listFiles();
|
||||
if (fileQuNum != null) {
|
||||
for (File fileNum : fileQuNum) {
|
||||
if (Objects.requireNonNull(fileNum.listFiles()).length > 0) {
|
||||
File fileJson = Objects.requireNonNull(fileNum.listFiles())[0];
|
||||
if (fileJson.isFile()
|
||||
&& fileJson.getName().contains("@EdgeDummy")
|
||||
&& fileJson.getName().toLowerCase().endsWith(".json")) {
|
||||
resultFile.add(fileJson);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
List<String> trueFileQuid = new ArrayList<>();
|
||||
// 开始读取文件并进行拆分
|
||||
for (ExamQuestion examQuestion : edgeList) {
|
||||
for (File file : resultFile) {
|
||||
String quId = file.getName().split("@")[0];
|
||||
if (quId.equals(examQuestion.getQuId())) {
|
||||
trueFileQuid.add(quId);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
// 查询哪些文件已经进行获取到学生作答文件了
|
||||
for (String str : trueFileQuid) {
|
||||
// 创建windows试题返回结果
|
||||
EdgeInfoVo edgeInfoVo = new EdgeInfoVo();
|
||||
// 查询到学生文件
|
||||
Optional<File> fileResult = resultFile.stream().filter(resultFiles -> resultFiles.getName().contains(str)).findFirst();
|
||||
if (fileResult.isPresent()) {
|
||||
File file = fileResult.get();
|
||||
// 创建 ObjectMapper 实例
|
||||
File jsonFile = new File(file.getPath());
|
||||
ObjectMapper objectMapper = new ObjectMapper();
|
||||
if (jsonFile.exists()) {
|
||||
// 获取试题ID
|
||||
String quId = file.getName().split("@")[0];
|
||||
edgeInfoVo.setQuId(quId);
|
||||
// 读取学生文件
|
||||
try (FileInputStream fileInputStream = new FileInputStream(jsonFile)) {
|
||||
// 读取文件并转换为 JsonNode
|
||||
JsonNode rootNode = objectMapper.readTree(fileInputStream);
|
||||
String jsonText = objectMapper.writeValueAsString(rootNode);
|
||||
// 从 JSON 文件读取并转换为 Person 对象
|
||||
Map<String, String> map = objectMapper.readValue(jsonText, Map.class);
|
||||
edgeInfoVo.setMap(map);
|
||||
edgeInfoVos.add(edgeInfoVo);
|
||||
// 关闭文件输入流
|
||||
if (fileInputStream != null) {
|
||||
try {
|
||||
fileInputStream.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return edgeInfoVos;
|
||||
}
|
||||
|
||||
// 将Map写入JSON文件
|
||||
// 将Map追加到JSON文件
|
||||
public static String writeMapToJson(Map<String, String> map, String filePath) {
|
||||
|
@@ -0,0 +1,16 @@
|
||||
package com.example.exam.exam.service.autoforbutton;
|
||||
|
||||
import com.example.exam.exam.controller.auto.vo.StuInfoVo;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @author REN
|
||||
*/
|
||||
public interface AutoForButtonService {
|
||||
|
||||
String autoForButtonToJson(StuInfoVo stuInfoVo);
|
||||
|
||||
Map<String, String> getButtonInfo(StuInfoVo stuInfoVo);
|
||||
}
|
@@ -0,0 +1,99 @@
|
||||
package com.example.exam.exam.service.autoforbutton;
|
||||
|
||||
import com.example.exam.exam.controller.auto.vo.StuInfoVo;
|
||||
import com.example.exam.exam.utils.c.LogFileUtils;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.io.*;
|
||||
import java.util.*;
|
||||
|
||||
@Service
|
||||
public class AutoForButtonServiceImpl implements AutoForButtonService {
|
||||
|
||||
/**
|
||||
* 学生端答题后按钮状态记录
|
||||
*
|
||||
* @param stuInfoVo 学生考试信息
|
||||
* @return 是否通过
|
||||
*/
|
||||
@Override
|
||||
public String autoForButtonToJson(StuInfoVo stuInfoVo) {
|
||||
// 试题ID
|
||||
String quId = stuInfoVo.getQuestionId();
|
||||
// 保存ID路径
|
||||
String filePath = stuInfoVo.getFilePath();
|
||||
File file = new File(filePath + "/Button.json");
|
||||
if (!file.exists()) {
|
||||
LogFileUtils.createHiddenFile(filePath + "/Button.json");
|
||||
}
|
||||
Map<String, String> map = new HashMap<>();
|
||||
map.put(quId, quId);
|
||||
return writeMapToJson(map, filePath + "/Button.json");
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, String> getButtonInfo(StuInfoVo stuInfoVo) {
|
||||
// 文件路径
|
||||
String filePath = stuInfoVo.getFilePath() + "/Button.json";
|
||||
// 读取JSON文件
|
||||
// 创建 ObjectMapper 实例
|
||||
File jsonFile = new File(filePath);
|
||||
ObjectMapper objectMapper = new ObjectMapper();
|
||||
Map<String, String> map = null;
|
||||
if (jsonFile.exists()) {
|
||||
try (FileInputStream fileInputStream = new FileInputStream(jsonFile)) {
|
||||
// 读取文件并转换为 JsonNode
|
||||
JsonNode rootNode = objectMapper.readTree(fileInputStream);
|
||||
String jsonText = objectMapper.writeValueAsString(rootNode);
|
||||
// 从 JSON 文件读取并转换为 Person 对象
|
||||
map = objectMapper.readValue(jsonText, Map.class);
|
||||
if (fileInputStream != null) {
|
||||
try {
|
||||
fileInputStream.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
return map;
|
||||
}
|
||||
|
||||
// 将Map写入JSON文件
|
||||
// 将Map追加到JSON文件
|
||||
public static String writeMapToJson(Map<String, String> map, String filePath) {
|
||||
ObjectMapper objectMapper = new ObjectMapper();
|
||||
File file = new File(filePath);
|
||||
|
||||
try {
|
||||
Map<String, String> existingMap = new HashMap<>();
|
||||
|
||||
if (file.exists() && file.length() > 0) {
|
||||
try (InputStream fis = new FileInputStream(file)) {
|
||||
existingMap = objectMapper.readValue(fis, Map.class);
|
||||
}
|
||||
}
|
||||
|
||||
existingMap.putAll(map);
|
||||
|
||||
try (OutputStream fos = new FileOutputStream(file, false);
|
||||
BufferedOutputStream bos = new BufferedOutputStream(fos)) {
|
||||
objectMapper.writeValue(bos, existingMap);
|
||||
bos.flush(); // 强制刷新
|
||||
}
|
||||
|
||||
// 可选占用测试
|
||||
boolean released = file.renameTo(file);
|
||||
System.out.println("文件释放状态: " + released);
|
||||
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return "学生答案已经写入到 " + filePath;
|
||||
}
|
||||
}
|
@@ -3,6 +3,7 @@ package com.example.exam.exam.service.autoforchoice;
|
||||
import com.example.exam.exam.controller.auto.vo.StuInfoVo;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @author REN
|
||||
@@ -12,4 +13,6 @@ public interface AutoForChoiceService {
|
||||
String autoForChoiceToJson(StuInfoVo stuInfoVo);
|
||||
|
||||
Double autoForChoice(StuInfoVo stuInfoVo) throws IOException;
|
||||
|
||||
Map<String, String> getChoiceInfo(StuInfoVo stuInfoVo);
|
||||
}
|
||||
|
@@ -297,6 +297,36 @@ public class AutoForChoiceServiceImpl implements AutoForChoiceService {
|
||||
return score;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, String> getChoiceInfo(StuInfoVo stuInfoVo) {
|
||||
// 文件路径
|
||||
String filePath = stuInfoVo.getFilePath() + "/Choice.json";
|
||||
// 读取JSON文件
|
||||
// 创建 ObjectMapper 实例
|
||||
File jsonFile = new File(filePath);
|
||||
ObjectMapper objectMapper = new ObjectMapper();
|
||||
Map<String, String> map = null;
|
||||
if (jsonFile.exists()) {
|
||||
try (FileInputStream fileInputStream = new FileInputStream(jsonFile)) {
|
||||
// 读取文件并转换为 JsonNode
|
||||
JsonNode rootNode = objectMapper.readTree(fileInputStream);
|
||||
String jsonText = objectMapper.writeValueAsString(rootNode);
|
||||
// 从 JSON 文件读取并转换为 Person 对象
|
||||
map = objectMapper.readValue(jsonText, Map.class);
|
||||
if (fileInputStream != null) {
|
||||
try {
|
||||
fileInputStream.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
return map;
|
||||
}
|
||||
|
||||
// 将Map写入JSON文件
|
||||
// 将Map追加到JSON文件
|
||||
public static String writeMapToJson(Map<String, String> map, String filePath) {
|
||||
|
Reference in New Issue
Block a user