"?" 입력시 인코딩 오류 수정
This commit is contained in:
@@ -3157,40 +3157,50 @@ public class PartMngService extends BaseService {
|
||||
// 시도할 인코딩 목록 (Windows Excel CSV 기본 인코딩 우선)
|
||||
String[] encodings = {"CP949", "UTF-8", "EUC-KR", "MS949"};
|
||||
|
||||
String bestEncoding = "UTF-8"; // 기본값
|
||||
int minReplacementChars = Integer.MAX_VALUE;
|
||||
|
||||
for (String encoding : encodings) {
|
||||
try (FileInputStream fis = new FileInputStream(file);
|
||||
InputStreamReader isr = new InputStreamReader(fis, encoding);
|
||||
BufferedReader br = new BufferedReader(isr)) {
|
||||
|
||||
// 처음 몇 줄을 읽어서 깨진 문자 여부 확인
|
||||
// 처음 몇 줄을 읽어서 깨진 문자 개수 확인
|
||||
String line;
|
||||
int lineCount = 0;
|
||||
boolean hasBrokenChar = false;
|
||||
int replacementCharCount = 0;
|
||||
|
||||
while ((line = br.readLine()) != null && lineCount < 10) {
|
||||
// 깨진 문자 검사 (<28> 또는 replacement character)
|
||||
if (line.contains("\uFFFD") || line.contains("?")) {
|
||||
hasBrokenChar = true;
|
||||
break;
|
||||
// replacement character (\uFFFD) 개수 세기
|
||||
for (int i = 0; i < line.length(); i++) {
|
||||
if (line.charAt(i) == '\uFFFD') {
|
||||
replacementCharCount++;
|
||||
}
|
||||
}
|
||||
lineCount++;
|
||||
}
|
||||
|
||||
// 깨진 문자가 없으면 이 인코딩이 올바른 것으로 판단
|
||||
if (!hasBrokenChar && lineCount > 0) {
|
||||
System.out.println("CSV 인코딩 감지 성공: " + encoding);
|
||||
// 깨진 문자가 없으면 바로 이 인코딩 사용
|
||||
if (replacementCharCount == 0 && lineCount > 0) {
|
||||
System.out.println("CSV 인코딩 감지 성공: " + encoding + " (깨진 문자 없음)");
|
||||
return encoding;
|
||||
}
|
||||
|
||||
// 깨진 문자가 가장 적은 인코딩 기억
|
||||
if (replacementCharCount < minReplacementChars) {
|
||||
minReplacementChars = replacementCharCount;
|
||||
bestEncoding = encoding;
|
||||
}
|
||||
|
||||
} catch (Exception e) {
|
||||
// 이 인코딩으로 읽기 실패 시 다음 인코딩 시도
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
// 모든 시도 실패 시 기본값 UTF-8 반환
|
||||
System.out.println("CSV 인코딩 감지 실패, 기본값 UTF-8 사용");
|
||||
return "UTF-8";
|
||||
// 가장 적은 깨진 문자를 가진 인코딩 사용
|
||||
System.out.println("CSV 인코딩 감지 완료: " + bestEncoding + " (replacement chars: " + minReplacementChars + ")");
|
||||
return bestEncoding;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user