fix: #I7VP0I 检测文件编码时尝试所有可能顺序

This commit is contained in:
songdragon 2023-08-24 18:01:58 +08:00
parent c4b998100e
commit f7505f6063

View File

@ -36,10 +36,12 @@ public class EncodingDetector {
CharsetDetector charsetDetector = new CharsetDetector(); CharsetDetector charsetDetector = new CharsetDetector();
try (BufferedInputStream inputStream = new BufferedInputStream(new FileInputStream(file.getPath()))) { try (BufferedInputStream inputStream = new BufferedInputStream(new FileInputStream(file.getPath()))) {
charsetDetector.setText(inputStream); charsetDetector.setText(inputStream);
CharsetMatch match = charsetDetector.detect(); CharsetMatch[] matchList = charsetDetector.detectAll();
LOG.debug("{} : {}", match.getName(), match.getConfidence()); for (CharsetMatch match : matchList) {
if (match.getConfidence() > 50) { LOG.debug("{} : {}", match.getName(), match.getConfidence());
return match.getName(); if (match.getConfidence() > 50) {
return match.getName();
}
} }
} catch (Exception e) { } catch (Exception e) {
LOG.error("", e); LOG.error("", e);