From 9d12d9ca1499c20137646736918a44a781934d79 Mon Sep 17 00:00:00 2001 From: leebj Date: Mon, 8 Jul 2024 11:30:33 +0900 Subject: [PATCH] =?UTF-8?q?=EC=BA=90=EB=A6=AD=ED=84=B0=EC=85=8B=20=20?= =?UTF-8?q?=ED=99=95=EC=9D=B8=20=EB=A9=94=EC=86=8C=EB=93=9C=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 7 +++ .../xit/fims/base/web/AdminController.java | 6 +- src/main/java/cokr/xit/fims/cmmn/Hangul.java | 55 +++++++++++++++++++ 3 files changed, 66 insertions(+), 2 deletions(-) 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); + } + } }