【新增】 读取C语言文件乱码问题

This commit is contained in:
dlaren
2025-08-20 22:16:38 +08:00
parent 184d1ce578
commit a74bd10f02
3 changed files with 43 additions and 1 deletions

View File

@@ -86,6 +86,13 @@
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.googlecode.juniversalchardet</groupId>
<artifactId>juniversalchardet</artifactId>
<version>1.0.3</version>
</dependency>
<!-- 工具类相关 -->
<dependency>

View File

@@ -29,6 +29,7 @@ import pc.exam.pp.module.exam.controller.admin.questionexamine.vo.QuestionExamin
import pc.exam.pp.module.exam.dal.dataobject.EducationPaperTask;
import pc.exam.pp.module.exam.dal.dataobject.ExamQuestion;
import pc.exam.pp.module.exam.service.question.IExamQuestionService;
import pc.exam.pp.module.exam.utils.charset.CharsetUtils;
import pc.exam.pp.module.system.enums.common.SexEnum;
import java.io.IOException;
@@ -295,7 +296,11 @@ public class ExamQuestionController {
if (!file.getFile().getOriginalFilename().endsWith(".c")) {
return CommonResult.error(100810, "请上传.c文件");
}
return CommonResult.success(new String(file.getFile().getBytes(), "GBK"));
byte[] bytes = file.getFile().getBytes();
String content = CharsetUtils.detectAndRead(bytes);
return CommonResult.success(content);
}
}

View File

@@ -0,0 +1,30 @@
package pc.exam.pp.module.exam.utils.charset;
import org.mozilla.universalchardet.UniversalDetector;
import java.io.IOException;
import java.nio.charset.Charset;
public class CharsetUtils {
/**
* 自动识别文件编码,并返回字符串内容
* @param bytes 文件字节数组
* @return 文件内容
*/
public static String detectAndRead(byte[] bytes) throws IOException {
// 1. 检测编码
UniversalDetector detector = new UniversalDetector(null);
detector.handleData(bytes, 0, bytes.length);
detector.dataEnd();
String detectedCharset = detector.getDetectedCharset();
// 2. 默认回退 UTF-8
if (detectedCharset == null) {
detectedCharset = "UTF-8";
}
// 3. 转换为字符串
return new String(bytes, Charset.forName(detectedCharset));
}
}