Skip to content

Commit

Permalink
部分gbk文件无法识别,加入新库
Browse files Browse the repository at this point in the history
  • Loading branch information
谢方振 authored and 谢方振 committed Dec 26, 2023
1 parent 8ac8cd8 commit be2e522
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 1 deletion.
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
<aspose-cad.version>23.7</aspose-cad.version>
<bcprov-jdk15on.version>1.70</bcprov-jdk15on.version>
<juniversalchardet.version>1.0.3</juniversalchardet.version>
<icu4j.version>70.1</icu4j.version>

<commons-cli.version>1.5.0</commons-cli.version>
<commons-net.version>3.9.0</commons-net.version>
Expand Down
5 changes: 5 additions & 0 deletions server/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,11 @@
<artifactId>juniversalchardet</artifactId>
<version>${juniversalchardet.version}</version>
</dependency>
<dependency>
<groupId>com.ibm.icu</groupId>
<artifactId>icu4j</artifactId>
<version>${icu4j.version}</version>
</dependency>

<!-- 解压(rar)-->
<dependency>
Expand Down
11 changes: 10 additions & 1 deletion server/src/main/java/cn/keking/utils/EncodingDetects.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package cn.keking.utils;

import com.ibm.icu.text.CharsetDetector;
import com.ibm.icu.text.CharsetMatch;
import org.mozilla.universalchardet.UniversalDetector;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -44,7 +46,14 @@ public static String getJavaEncode(byte[] content) {
detector.dataEnd();
String charsetName = detector.getDetectedCharset();
if (charsetName == null) {
charsetName = Charset.defaultCharset().name();
CharsetDetector cd = new CharsetDetector();
cd.setText(content);
CharsetMatch cm = cd.detect();
if (cm != null) {
charsetName = cm.getName();
} else {
charsetName = Charset.defaultCharset().name();
}
}
return charsetName;
}
Expand Down

0 comments on commit be2e522

Please sign in to comment.