티스토리 툴바


한글을 전송 시 깨져서 삽질하는 경우가 참 많다.
전송 할때 한글 문자를 아스키 코드로 변환하여 전송뒤 받을때 아스키 코드를 풀어 쓰는 방법으로 송수신 문자셋에 제약없이 사용 가능하다.

escape
public static String escape(String src){
	int i;
	char j;
	StringBuffer tmp = new StringBuffer();
	tmp.ensureCapacity(src.length() * 6);
	for (i = 0; i < src.length(); i++) {
		j = src.charAt(i);
		if (Character.isDigit(j) || Character.isLowerCase(j)
		|| Character.isUpperCase(j))
			tmp.append(j);
		else if (j < 256) {
			tmp.append("\\");
			if (j < 16)
				tmp.append("0");
			tmp.append(Integer.toString(j, 16));
		} else {
			tmp.append("\\u");
			tmp.append(Integer.toString(j, 16));
		}
	}
	return tmp.toString();
}


unescape
public static String unescape(String src) {
	StringBuffer tmp = new StringBuffer();
	tmp.ensureCapacity(src.length());
	int lastPos = 0, pos = 0;
	char ch;
	while (lastPos < src.length()) {
		pos = src.indexOf("\\", lastPos);
		if (pos == lastPos) {
			if (src.charAt(pos + 1) == 'u') {
				ch = (char) Integer.parseInt(src
				.substring(pos + 2, pos + 6), 16);
				tmp.append(ch);
				lastPos = pos + 6;
			} else {
				ch = (char) Integer.parseInt(src
				.substring(pos + 1, pos + 3), 16);
				tmp.append(ch);
				lastPos = pos + 3;
			}
		} else {
			if (pos == -1) {
				tmp.append(src.substring(lastPos));
				lastPos = src.length();
			} else {
				tmp.append(src.substring(lastPos, pos));
				lastPos = pos;
			}
		}
	}
	return tmp.toString();
}
저작자 표시 비영리 변경 금지
Posted by 페닐

댓글을 달아 주세요

  1. Favicon of http://fbwotjq.tistory.com BlogIcon 류재섭 2011/12/04 12:03  댓글주소  수정/삭제  댓글쓰기

    아악 멋쟁이 ㅋㅋㅋ

String sso = "F1=test1;F2=test2;F3=test3;";

while (sso.indexOf(";") != -1){
  String strTemp = sso.substring(0, sso.indexOf(";"));
  sso = sso.substring(sso.indexOf(";")+1);
  String key = strTemp.substring(0,strTemp.indexOf("="));
  String value = strTemp.substring(strTemp.indexOf("=")+1);
  if (key.equals("F1")) {
   f1 = value;                  
  }else if(key.equals("F2")) {
   f2 = value;
  }
 }
저작자 표시 비영리 변경 금지
Posted by 페닐

댓글을 달아 주세요

import java.io.IOException;
import java.io.UnsupportedEncodingException;

import sun.misc.BASE64Decoder;  
import sun.misc.BASE64Encoder;  
  
/** 
 * Test Base64 Encoding 
 *  
 * @author zenobio 
 */  
public class TestBase64 {  
  
    /** 
     * @param args 
     */  
    public static void main(String[] args) {  
        // TODO Auto-generated method stub  
        BASE64Encoder encoder = new BASE64Encoder();  
        BASE64Decoder decoder = new BASE64Decoder();  
          
        //String plainText = "BASE64 인코딩할 문자열...";  
        String plainText = "가나다라마바사";
          
        try{  
            String encText = encode(encoder, plainText);  
              
            decode(decoder, encText);              
        	//decode(decoder, plainText);
        }catch(Exception e){  
            e.printStackTrace();  
        }  
    }

	private static String encode(BASE64Encoder encoder, String plainText)
			throws UnsupportedEncodingException {
		//인코딩 하기전 문자열 출력  
		System.out.println("orginal text:" + plainText);  
		  
		//BASE64 인코딩 수행  
		String encText = encoder.encode(plainText.getBytes("utf-8"));  
		//인코딩된 문자열 출력  
		System.out.println("encoded text:" + encText);
		return encText;
	}

	private static void decode(BASE64Decoder decoder, String encText)
			throws IOException, UnsupportedEncodingException {
		//디코딩 수행  
		byte[] decode = decoder.decodeBuffer(encText);  
		//바이트배열을 문자열로 치환  
		String decText = new String(decode, "utf-8");  
		//디코딩된 문자열 출력  
		System.out.println("decoded text:" + decText);
	}  
  
}  
저작자 표시 비영리 변경 금지
Posted by 페닐

댓글을 달아 주세요

AIX에 MYSQL 깔기

Database 2011/07/04 18:18
1. mysql-source 버젼 다운 받기
 
2. mv mysql-5.X.X.tar.gz /usr/local
 
3. tar xvzf mysql-5.X.X.X.tar.gz
 
4. cd /usr/local/mysql-5.X.X.X
 
5. ./configure --prefix=/usr/local/mysql --localstatedir=/usr/local/mysql/data --with-charset=utf8 --with-mysql-usr=mysql --sysconfdir=/etc --enable-thread-safe-client
 
./configure --prefix=/usr/local/mysql --localstatedir=/usr/local/mysql/data --with-charset=euckr --with-mysql-usr=mysql --sysconfdir=/etc --enable-thread-safe-client
 
./configure --prefix=/home/wisenut/sf-1/mysql --localstatedir=/home/wisenut/sf-1/mysql/data --with-charset=utf8 --with-mysql-usr=wisenut --sysconfdir=/etc --enable-thread-safe=client
 
./configure --prefix=/home/kwisenut/sf-1v4.5/mysql/mysql5 --localstatedir=/home/kwisenut/sf-1v4.5/mysql/mysql5/data --with-charset=utf8 --with-mysql-usr=kwisenut --sysconfdir=/etc --enable-thread-safe=client
 
./configure --prefix=/home/kwisenut/sf-1v4.5/mysql/mysql-5.1.17 --localstatedir=/home/kwisenut/sf-1v4.5/mysql/mysql-5.1.17/data --with-charset=utf8 
 
6. make
 
7. make install
 
8. ./scripts/mysql_install_db
 
9. cat >> /etc/ld.so.conf
/usr/local/mysql/lib (엔터치고 ctrl+c 입력)
 
10. ldconfig
 
11. cp /usr/local/mysql/share/mysql/my-medium.cnf /etc/my.cnf
 
12. mysql 계정 생성
groupadd mysql
useradd -g mysql mysql
chown root.mysql -R /usr/local/mysql
chown mysql.mysql -R /usr/local/mysql/data
chgrp -R mysql /usr/local/mysql
 
 
13. /usr/local/mysql/bin/mysqld_safe --user=mysql &
 
14. /usr/local/mysql/bin/mysql
     use mysql
     update user set password=password('root!@')
     update user set password=password('root')
     where user = 'root';
     delete from user where user='';
     flush privileges;
 
15. /usr/local/mysql/bin/mysqladmin -u root -p showdown
 
16. /usr/local/mysql/bin/msyqld_safe -u root &

저작자 표시 비영리 변경 금지
Posted by 페닐

댓글을 달아 주세요

request로 들어온 값들을 일일이 vo 객체에 set할필요없이 한번의 함수 호출로 전부 set됨

private void bind(HttpServletRequest request, Object vo) {
   Method[] methods = vo.getClass().getMethods();      //vo의 메소드를 구한다
   for (Method method : methods) {         //메소드들을 루프
    if(method.getName().startsWith("set")) {     //메소드가 set으로 시작한다면?
     String propertyName = method.getName().substring(3);  //앞에 set문자 짜르기
     propertyName = propertyName.toLowerCase();     //set뒤의 문자 소문자로 변경
     if (method.getParameterTypes()[0].getSimpleName().equals("String")) {  //setter메소드의 첫번째파라메터 타입이 string이라면
      try {
       method.invoke(vo, request.getParameter(propertyName));    // 그 메소드를 실행
      } catch (Exception e) {
       e.printStackTrace();
      } 
     } /*else if(method.getParameterTypes()[0].getSimpleName().equals("int")){
      try {
       method.invoke(vo, Integer.parseInt(request.getParameter(propertyName)));
      } catch (Exception e) {
       e.printStackTrace();
      }
     }*/
    }
   }
 } 
저작자 표시 비영리 변경 금지
Posted by 페닐

댓글을 달아 주세요