diff --git a/pom.xml b/pom.xml index 80772f2b..0666096c 100644 --- a/pom.xml +++ b/pom.xml @@ -94,6 +94,13 @@ runtime + + + com.ibm.icu + icu4j + 75.1 + + cokr.xit.interfaces diff --git a/src/main/java/cokr/xit/fims/base/web/AdminController.java b/src/main/java/cokr/xit/fims/base/web/AdminController.java index a62a3759..fec91880 100644 --- a/src/main/java/cokr/xit/fims/base/web/AdminController.java +++ b/src/main/java/cokr/xit/fims/base/web/AdminController.java @@ -27,6 +27,7 @@ import cokr.xit.fims.base.Backup; import cokr.xit.fims.base.service.bean.AdminBean; import cokr.xit.fims.cmmn.DirectoryStructureToJson; import cokr.xit.fims.cmmn.DirectoryStructureToJson.Node; +import cokr.xit.fims.cmmn.Hangul; import cokr.xit.fims.crdn.service.ImportService; import cokr.xit.foundation.data.DataObject; import cokr.xit.interfaces.smg.service.SmgService; @@ -181,8 +182,9 @@ public class AdminController extends ApplicationController { int inputData = 0; try { - - BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(path),Charset.forName("EUC-KR"))); + Hangul hangul = new Hangul(3); + String detect = hangul.detect(path); + BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(path),Charset.forName(detect))); while ((inputData = br.read()) != -1) { sb.append((char)inputData) ; diff --git a/src/main/java/cokr/xit/fims/cmmn/Hangul.java b/src/main/java/cokr/xit/fims/cmmn/Hangul.java index 7756bf4b..2183782c 100644 --- a/src/main/java/cokr/xit/fims/cmmn/Hangul.java +++ b/src/main/java/cokr/xit/fims/cmmn/Hangul.java @@ -1,5 +1,14 @@ package cokr.xit.fims.cmmn; +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; + +import org.apache.commons.io.IOUtils; + +import com.ibm.icu.text.CharsetDetector; +import com.ibm.icu.text.CharsetMatch; + public class Hangul { public Hangul(int hangulIsNByte){ @@ -159,4 +168,50 @@ public class Hangul { return str.substring(beginIndex); } + + /** + * 텍스트 인코딩 확인 + * + * @param path + * @return 캐릭터셋 + */ + public String encodingDetect(String path) throws IOException { + File f = new File(path); + + return encodingDetect(f); + } + + public String encodingDetect(File f) throws IOException { + CharsetDetector detector; + CharsetMatch match; + + FileInputStream fis = null; + try { + String result = ""; + + fis = new FileInputStream(f); + + byte[] byteData = new byte[(int) f.length()]; + + fis.read(byteData); + fis.close(); + detector = new CharsetDetector(); + + detector.setText(byteData); + match = detector.detect(); + + System.out.println("encoding is \"" + match.getName() + "\""); + + if(match.getName().equals("UTF-8") || match.getName().equals("EUC-KR")) { + result = match.getName(); + } else { + result = "EUC-KR"; + } + + return result; + } + finally { + IOUtils.closeQuietly(fis); + } + } }