Merge branch 'main' into feature/new-work
This commit is contained in:
@@ -3149,6 +3149,50 @@ public class PartMngService extends BaseService {
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* CSV 파일의 인코딩을 자동으로 감지합니다.
|
||||
* UTF-8 → CP949 → EUC-KR 순서로 시도
|
||||
*/
|
||||
private String detectFileEncoding(File file) {
|
||||
// 시도할 인코딩 목록 (Windows Excel CSV 기본 인코딩 우선)
|
||||
String[] encodings = {"CP949", "UTF-8", "EUC-KR", "MS949"};
|
||||
|
||||
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;
|
||||
|
||||
while ((line = br.readLine()) != null && lineCount < 10) {
|
||||
// 깨진 문자 검사 (<28> 또는 replacement character)
|
||||
if (line.contains("\uFFFD") || line.contains("?")) {
|
||||
hasBrokenChar = true;
|
||||
break;
|
||||
}
|
||||
lineCount++;
|
||||
}
|
||||
|
||||
// 깨진 문자가 없으면 이 인코딩이 올바른 것으로 판단
|
||||
if (!hasBrokenChar && lineCount > 0) {
|
||||
System.out.println("CSV 인코딩 감지 성공: " + encoding);
|
||||
return encoding;
|
||||
}
|
||||
|
||||
} catch (Exception e) {
|
||||
// 이 인코딩으로 읽기 실패 시 다음 인코딩 시도
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
// 모든 시도 실패 시 기본값 UTF-8 반환
|
||||
System.out.println("CSV 인코딩 감지 실패, 기본값 UTF-8 사용");
|
||||
return "UTF-8";
|
||||
}
|
||||
|
||||
/**
|
||||
* CSV 파일 파싱 (엑셀과 동일한 형식으로 반환)
|
||||
* 첫 번째 열이 "수준"인 경우 계층 구조를 자동으로 파악
|
||||
@@ -3159,15 +3203,21 @@ public class PartMngService extends BaseService {
|
||||
|
||||
try {
|
||||
File csvFile = new File(path + "\\" + fileName);
|
||||
// UTF-8 BOM 처리를 위한 InputStreamReader 설정
|
||||
|
||||
// 인코딩 자동 감지: UTF-8 → CP949 → EUC-KR 순서로 시도
|
||||
String detectedEncoding = detectFileEncoding(csvFile);
|
||||
System.out.println("CSV 파일 인코딩 감지: " + detectedEncoding);
|
||||
|
||||
FileInputStream fis = new FileInputStream(csvFile);
|
||||
InputStreamReader isr = new InputStreamReader(fis, "UTF-8");
|
||||
InputStreamReader isr = new InputStreamReader(fis, detectedEncoding);
|
||||
br = new BufferedReader(isr);
|
||||
|
||||
// UTF-8 BOM 제거 (EF BB BF)
|
||||
br.mark(1);
|
||||
if (br.read() != 0xFEFF) {
|
||||
br.reset(); // BOM이 아니면 처음으로 되돌림
|
||||
// UTF-8 BOM 제거 (EF BB BF) - UTF-8인 경우에만
|
||||
if ("UTF-8".equals(detectedEncoding)) {
|
||||
br.mark(1);
|
||||
if (br.read() != 0xFEFF) {
|
||||
br.reset(); // BOM이 아니면 처음으로 되돌림
|
||||
}
|
||||
}
|
||||
|
||||
String line;
|
||||
|
||||
Reference in New Issue
Block a user