Skip to content

Instantly share code, notes, and snippets.

@kenu
Created March 3, 2020 02:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kenu/51e05999e5cdd5824ae2956ea0641add to your computer and use it in GitHub Desktop.
Save kenu/51e05999e5cdd5824ae2956ea0641add to your computer and use it in GitHub Desktop.
decodeAny
package com.okdevtv;
import static org.junit.Assert.*;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import org.junit.Test;
public class KrDecoderTest {
@Test
public void testDecodeEuckr() {
String urlencoded = "test%40test.com";
assertEquals("test@test.com", decode(urlencoded));
String hangul = "%C1%A4%BB%F3%C3%B3%B8%AE+%B5%C7%BE%FA%BD%C0%B4%CF%B4%D9.";
assertEquals("정상처리 되었습니다.", decode(hangul));
String hangulSpace = "%C1%A4%BB%F3%C3%B3%B8%AE%20%B5%C7%BE%FA%BD%C0%B4%CF%B4%D9.";
assertEquals("정상처리 되었습니다.", decode(hangulSpace));
}
@Test
public void testDecodeUtf8() {
String urlencoded = "test%40test.com";
assertEquals("test@test.com", decodeUtf8(urlencoded));
String hangul = "%EC%A0%95%EC%83%81%EC%B2%98%EB%A6%AC+%EB%90%98%EC%97%88%EC%8A%B5%EB%8B%88%EB%8B%A4.";
assertEquals("정상처리 되었습니다.", decodeUtf8(hangul));
String hangulSpace = "%EC%A0%95%EC%83%81%EC%B2%98%EB%A6%AC%20%EB%90%98%EC%97%88%EC%8A%B5%EB%8B%88%EB%8B%A4.";
assertEquals("정상처리 되었습니다.", decodeUtf8(hangulSpace));
}
@Test
public void testDecodeMix() {
String urlencoded = "test%40test.com";
assertEquals("test@test.com", decodeUtf8(urlencoded));
String hangul = "%C1%A4%BB%F3%C3%B3%B8%AE+%B5%C7%BE%FA%BD%C0%B4%CF%B4%D9.";
String decodeUtf8 = decodeUtf8(hangul);
assertEquals("����ó�� �Ǿ����ϴ�.", decodeUtf8);
String hangulSpace = "%EC%A0%95%EC%83%81%EC%B2%98%EB%A6%AC%20%EB%90%98%EC%97%88%EC%8A%B5%EB%8B%88%EB%8B%A4.";
assertEquals("����泥�由� �����듬����.", decode(hangulSpace));
}
@Test
public void testDecodeAny() {
String urlencoded = "test%40test.com";
assertEquals("test@test.com", decodeUtf8(urlencoded));
String hangul = "%C1%A4%BB%F3%C3%B3%B8%AE+%B5%C7%BE%FA%BD%C0%B4%CF%B4%D9.";
String decodeUtf8 = decodeAny(hangul);
assertEquals("정상처리 되었습니다.", decodeUtf8);
String hangulSpace = "%EC%A0%95%EC%83%81%EC%B2%98%EB%A6%AC%20%EB%90%98%EC%97%88%EC%8A%B5%EB%8B%88%EB%8B%A4.";
assertEquals("정상처리 되었습니다.", decodeAny(hangulSpace));
}
public String decodeAny(String urlencoded) {
String decode = decode(urlencoded);
String ch = String.valueOf((char)65533);
if (decode.indexOf(ch) > -1) {
decode = decodeUtf8(urlencoded);
}
return decode;
}
public String decode(String urlencoded) {
String decode = null;
try {
decode = URLDecoder.decode(urlencoded, "EUC-KR");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return decode;
}
public String decodeUtf8(String urlencoded) {
String decode = null;
try {
decode = URLDecoder.decode(urlencoded, "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return decode;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment