diff --git a/src/main/java/com/example/exam/exam/controller/auto/AutoController.java b/src/main/java/com/example/exam/exam/controller/auto/AutoController.java index 4fc4df1..29de78c 100644 --- a/src/main/java/com/example/exam/exam/controller/auto/AutoController.java +++ b/src/main/java/com/example/exam/exam/controller/auto/AutoController.java @@ -73,7 +73,7 @@ public class AutoController { * @return 判分结果 */ @PostMapping("/judgementForC") - public Result judgementForC(@RequestBody StuInfoVo stuInfoVo){ + public Result judgementForC(@RequestBody StuInfoVo stuInfoVo) throws IOException { return Result.success(autoService.autoForC(stuInfoVo)); } // WPS-WORD diff --git a/src/main/java/com/example/exam/exam/service/autoforc/AutoForCService.java b/src/main/java/com/example/exam/exam/service/autoforc/AutoForCService.java index 403536f..934ce61 100644 --- a/src/main/java/com/example/exam/exam/service/autoforc/AutoForCService.java +++ b/src/main/java/com/example/exam/exam/service/autoforc/AutoForCService.java @@ -2,6 +2,7 @@ package com.example.exam.exam.service.autoforc; import com.example.exam.exam.controller.auto.vo.StuInfoVo; +import java.io.IOException; import java.math.BigDecimal; /** @@ -13,5 +14,5 @@ public interface AutoForCService { * 自动判题 C语言 * @param stuInfoVo 学生信息 */ - BigDecimal autoForC(StuInfoVo stuInfoVo); + BigDecimal autoForC(StuInfoVo stuInfoVo) throws IOException; } diff --git a/src/main/java/com/example/exam/exam/service/autoforc/AutoForCServiceImpl.java b/src/main/java/com/example/exam/exam/service/autoforc/AutoForCServiceImpl.java index 8064c03..02a028e 100644 --- a/src/main/java/com/example/exam/exam/service/autoforc/AutoForCServiceImpl.java +++ b/src/main/java/com/example/exam/exam/service/autoforc/AutoForCServiceImpl.java @@ -12,6 +12,7 @@ import jakarta.annotation.Resource; import org.springframework.stereotype.Service; import java.io.File; +import java.io.IOException; import java.math.BigDecimal; import java.util.List; import java.util.Optional; @@ -37,7 +38,7 @@ public class AutoForCServiceImpl implements AutoForCService { * @return 是否通过 */ @Override - public BigDecimal autoForC(StuInfoVo stuInfoVo) { + public BigDecimal autoForC(StuInfoVo stuInfoVo) throws IOException { SystemTenant systemTenant = systemTenantService.getId(stuInfoVo.getSchoolName()); BigDecimal score = new BigDecimal(0); // 0、获取到试卷信息(试卷详情) @@ -67,10 +68,11 @@ public class AutoForCServiceImpl implements AutoForCService { EducationPaperQu educationPaperQu = results.get(); String quScore = educationPaperScheme.getQuScores(); ExamQuestion examQuestion = examQuestionService.selectExamQuestionByQuId(quId); - System.out.println(wjFile); File[] lastFiles = wjFile.listFiles(); for (File lastFile : lastFiles) { + System.out.println(lastFile); if (lastFile.getName().contains(".c")) { + System.out.println("开始C语言判分"+quNumber); String judgementStr = "

-----------------------------------------------------------

"; judgementStr += "

试题序号:" + educationPaperQu.getSort() + "

"; judgementStr += "

试题编号:" + examQuestion.getQuNum() + "

"; diff --git a/src/main/java/com/example/exam/exam/service/c/JudgementService.java b/src/main/java/com/example/exam/exam/service/c/JudgementService.java index add3186..b4697d2 100644 --- a/src/main/java/com/example/exam/exam/service/c/JudgementService.java +++ b/src/main/java/com/example/exam/exam/service/c/JudgementService.java @@ -4,6 +4,8 @@ package com.example.exam.exam.service.c; import com.example.exam.exam.dal.ExamQuestion; import com.example.exam.exam.dal.SourceAndText; +import java.io.IOException; + /** * 判分逻辑集合 * @@ -19,7 +21,7 @@ public interface JudgementService { * @param score 分数 * @return 返回判分 */ - public SourceAndText ProgrammingC(double score, String pathC, String fileName, ExamQuestion examQuestion, String judgementStr); + public SourceAndText ProgrammingC(double score, String pathC, String fileName, ExamQuestion examQuestion, String judgementStr) throws IOException; } diff --git a/src/main/java/com/example/exam/exam/service/c/JudgementServiceImpl.java b/src/main/java/com/example/exam/exam/service/c/JudgementServiceImpl.java index 29d06a9..eaee986 100644 --- a/src/main/java/com/example/exam/exam/service/c/JudgementServiceImpl.java +++ b/src/main/java/com/example/exam/exam/service/c/JudgementServiceImpl.java @@ -9,6 +9,7 @@ import com.example.exam.exam.utils.c.LogFileUtils; import org.springframework.stereotype.Service; import com.example.exam.exam.dal.SourceAndText; +import java.io.IOException; import java.util.*; /** @@ -28,7 +29,7 @@ public class JudgementServiceImpl implements JudgementService * @return 返回判分 */ @Override - public SourceAndText ProgrammingC(double score, String pathC, String fileName, ExamQuestion examQuestion, String judgementStr) { + public SourceAndText ProgrammingC(double score, String pathC, String fileName, ExamQuestion examQuestion, String judgementStr) throws IOException { SourceAndText sourceAndText = new SourceAndText(); // 关键字比对,超过权重-测试用例/运行(测试用例全对,直接满分-不全对)-结果,不超过权重只给关键字几个的分 // 获取该题有多少分 @@ -66,6 +67,7 @@ public class JudgementServiceImpl implements JudgementService // 创建log文件txt,用于记录 // LogFileUtils.createFile(pathC + "/log.txt"); + System.out.println(pathC + " --- " + fileName); String code = JudgementCUtils.readFile(pathC, fileName); // LogFileUtils.writeLine("✅ 系统开始读取学生考试文件:" + code); judgementStr = HtmlAppender.appendHtmlLine(judgementStr, "✅ 系统开始读取学生考试文件:" + code); diff --git a/src/main/java/com/example/exam/exam/utils/c/JudgementCUtils.java b/src/main/java/com/example/exam/exam/utils/c/JudgementCUtils.java index 6e7fb14..638e8ba 100644 --- a/src/main/java/com/example/exam/exam/utils/c/JudgementCUtils.java +++ b/src/main/java/com/example/exam/exam/utils/c/JudgementCUtils.java @@ -2,7 +2,10 @@ package com.example.exam.exam.utils.c; import javax.swing.*; import java.io.*; +import java.nio.charset.Charset; +import java.nio.charset.MalformedInputException; import java.nio.charset.StandardCharsets; +import java.nio.charset.UnmappableCharacterException; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; @@ -190,35 +193,53 @@ public class JudgementCUtils * @param filePath 文件路径 * @return 文件内容 */ - public static String readFile(String filePath, String fileName) { - // 创建一个 Path 对象,指向包含 C 语言文件的文件夹 - Path folderPath = Paths.get(filePath); + public static String readFile(String filePath, String fileName) throws IOException { + Path folderPath = Paths.get(filePath).normalize().toAbsolutePath(); - // 判断路径是否存在 if (!Files.exists(folderPath)) { - return null; // 或者 return null,或者 throw new RuntimeException(...),看你的函数定义 + throw new FileNotFoundException("路径不存在: " + folderPath); } - // 使用 StringBuilder 来累积所有读取到的代码内容 StringBuilder codeBuilder = new StringBuilder(); + final boolean[] fileFound = {false}; try (Stream paths = Files.walk(folderPath)) { paths.filter(Files::isRegularFile) - .filter(path -> path.getFileName().toString().equalsIgnoreCase(fileName)) + .filter(path -> path.getFileName().toString().equals(fileName)) // 精确匹配 .forEach(path -> { + fileFound[0] = true; try { - List lines = Files.readAllLines(path, StandardCharsets.UTF_8); - for (String line : lines) { - codeBuilder.append("\n").append(line); + Charset charset = detectCharset(path); + List lines = Files.readAllLines(path, charset); + lines.forEach(line -> { + codeBuilder.append(line).append("\n"); System.out.println(" " + line); - } + }); } catch (IOException e) { -// LogFileUtils.writeLine("❗ 无法读取文件: " + path); + throw new UncheckedIOException("无法读取文件: " + path, e); } }); - } catch (IOException e) { - return null; } + + if (!fileFound[0]) { + throw new FileNotFoundException("未找到文件: " + fileName + " 在路径: " + folderPath); + } + return codeBuilder.toString(); } + private static Charset detectCharset(Path path) throws IOException { + // 常见中文编码列表 + Charset[] candidates = {StandardCharsets.UTF_8, Charset.forName("GBK"), + StandardCharsets.ISO_8859_1, StandardCharsets.UTF_16}; + + for (Charset charset : candidates) { + try (BufferedReader reader = Files.newBufferedReader(path, charset)) { + reader.read(); + return charset; // 如果能正常读取第一个字符,可能是正确编码 + } catch (MalformedInputException | UnmappableCharacterException e) { + // 尝试下一个编码 + } + } + return StandardCharsets.UTF_8; // 默认回退 + } }