diff --git a/src/main/java/com/example/exam/exam/service/mysql/IMysqlLocalServiceImpl.java b/src/main/java/com/example/exam/exam/service/mysql/IMysqlLocalServiceImpl.java index 97ea8db..faff587 100644 --- a/src/main/java/com/example/exam/exam/service/mysql/IMysqlLocalServiceImpl.java +++ b/src/main/java/com/example/exam/exam/service/mysql/IMysqlLocalServiceImpl.java @@ -21,6 +21,8 @@ import org.springframework.stereotype.Service; import java.io.*; import java.math.BigDecimal; import java.math.RoundingMode; +import java.nio.charset.Charset; +import java.nio.charset.MalformedInputException; import java.nio.charset.StandardCharsets; import java.nio.file.DirectoryStream; import java.nio.file.Files; @@ -520,14 +522,14 @@ public class IMysqlLocalServiceImpl implements IMysqlLocalService { .filter(Files::isRegularFile) .forEach(filePaths -> { try { - String fileContent = Files.readString(filePaths, StandardCharsets.UTF_8); + String fileContent = readFileContentAutoCharset(filePaths.toString()); String normalizedFileContent = fileContent.trim().replaceAll("\\s+", "").toLowerCase(); String normalizedFinalSql = finalSql.trim().replaceAll("\\s+", "").toLowerCase(); if (normalizedFinalSql.equals(normalizedFileContent)) { String stuPath=fileStu+"\\"+filePaths.getFileName(); fileName.set(String.valueOf(filePaths.getFileName())); - stuAnswer.set(readSQLFromFile(stuPath)); + stuAnswer.set(readFileContentAutoCharset(stuPath)); System.out.println("考生语句"+stuAnswer); } } catch (Exception e) { @@ -578,7 +580,12 @@ public class IMysqlLocalServiceImpl implements IMysqlLocalService { } boolean isEquivalent =false; + System.out.println("========================================================="); +// +// judgementStr = HtmlAppender.appendHtmlLine(judgementStr,"正确答案:"+answerList); +// judgementStr = HtmlAppender.appendHtmlLine(judgementStr,"考生答案:"+answerListStu); + System.out.println("========================================================="); if (answerListStu!=null&&answerListStu.size()>0){ isEquivalent = compareResultsSelect(answerList, answerListStu); } @@ -716,14 +723,14 @@ public class IMysqlLocalServiceImpl implements IMysqlLocalService { .filter(Files::isRegularFile) .forEach(filePaths -> { try { - String fileContent = Files.readString(filePaths, StandardCharsets.UTF_8); + String fileContent = readFileContentAutoCharset(filePaths.toString()); String normalizedFileContent = fileContent.trim().replaceAll("\\s+", "").toLowerCase(); String normalizedFinalSql = finalSql.trim().replaceAll("\\s+", "").toLowerCase(); if (normalizedFinalSql.equals(normalizedFileContent)) { String stuPath=fileStu+"\\"+filePaths.getFileName(); - stuAnswer.set(readSQLFromFile(stuPath)); fileName.set(String.valueOf(filePaths.getFileName())); + stuAnswer.set(readFileContentAutoCharset(stuPath)); System.out.println("考生语句"+stuAnswer); } } catch (Exception e) { @@ -731,8 +738,6 @@ public class IMysqlLocalServiceImpl implements IMysqlLocalService { e.printStackTrace(); } } - - ); } catch (IOException e) { appendToFile(answerLogPath,"遍历目录出错!"); @@ -1085,6 +1090,15 @@ public class IMysqlLocalServiceImpl implements IMysqlLocalService { sourceAndText.setText(judgementStr); return sourceAndText; } + private static String readFileContentAutoCharset(String filePath) throws IOException { + Path path = Paths.get(filePath); + try { + return Files.readString(path, StandardCharsets.UTF_8); + } catch (MalformedInputException e) { + System.out.println("文件【" + filePath + "】不是UTF-8编码,尝试使用GBK编码读取..."); + return Files.readString(path, Charset.forName("GBK")); + } + } @Override public void JudgementFile(File mysqlFile, ExamQuestion examQuestion) {