한글 이미지파일 처리
사이트를 돌아다니다보면 이미지가 'x' 표시로 보여지거나 혹은 아예 안보이는 경우가 있다.
이때 URLEncoder Class를 사용해서 한글 이미지를 처리해주자. ( JSP 용 )
<%@ page language="java" contentType="text/html; charset=euc-kr" %>
<%@ page import=" java.net.*" %>
<table>
<tr>
<td><img src="eknote.png"><td> <!-- 영문 : 아무문제 없다. -->
</tr>
<tr>
<td>
<img src="이케이노트.png">< !-- 한글 : 문제가 있을수 있다. -->
<img src="<%=URLEncoder.encode("이케이노트.png","euc-kr")%>">< !-- 한글처리; -->
<td>
</tr>
</table>
☞ URLEncoder API 보러가기
- 스크립트 처리를 위해 블러거가 좋은 자료를 공개해 놓았다. API 설명 찾으려다가. ㅋ
( 뒷걸음질 치다가..똥 밟은격? 냐하하하하 엽기적인 비유닷..쿄쿄 )
- 출처 : http://blog.naver.com/lsv400?Redirect=Log&logNo=100039245392 javascript의 decodeURI() 는 java.net.URLEncoder.encode()와 치환에 사용하는 문자열셋이
다르므로 , 와 같은 문자를 제대로 디코딩하지 못하는 문제가 발생하게 된다.
이를 해결하기 위해 java.net.URLDecoder.decode()와 동일한 알고리즘을 가지는 javascript함수를 사용한다.
function decodeURL(str){
var s0, i, j, s, ss, u, n, f;
s0 = ""; // decoded str
for (i = 0; i < str.length; i++){ // scan the source str
s = str.charAt(i);
if (s == "+"){s0 += " ";} // "+" should be changed to SP
else {
if (s != "%"){s0 += s;} // add an unescaped char
else{ // escape sequence decoding
u = 0; // unicode of the character
f = 1; // escape flag, zero means end of this sequence
while (true) {
ss = ""; // local str to parse as int
for (j = 0; j < 2; j++ ) { // get two maximum hex characters for parse
sss = str.charAt(++i);
if (((sss >= "0") && (sss <= "9")) || ((sss >= "a")
&& (sss <= "f")) || ((sss >= "A") && (sss <=
"F"))) {
ss += sss; // if hex, add the hex character
} else {--i; break;} // not a hex char., exit the loop
}
n = parseInt(ss, 16); // parse the hex str as byte
if (n <= 0x7f){u = n; f = 1;} // single byte format
if ((n >= 0xc0) && (n <= 0xdf)){u = n & 0x1f; f = 2;} // double byte format
if ((n >= 0xe0) && (n <= 0xef)){u = n & 0x0f; f = 3;} // triple byte format
if ((n >= 0xf0) && (n <= 0xf7)){u = n & 0x07; f =
4;} // quaternary byte format (extended)
if
((n >= 0x80) && (n <= 0xbf)){u = (u << 6) + (n
& 0x3f); --f;} // not a first, shift and add 6 lower bits
if (f <= 1){break;} // end of the utf byte sequence
if (str.charAt(i + 1) == "%"){ i++ ;} // test for the next shift byte
else {break;} // abnormal, format error
}
s0 += String.fromCharCode(u); // add the escaped character
}
}
}
return s0;
}