ext ajax文字化けし問題


1.コミット時に(jsで)escapeを使用し、%u 33 ea(unicodeコード)のように回転する
2.バックグラウンドでunicodeコードをGBK(以下実行可能クラスを示す)に変換し、データベースがiso-8859-1を使用する場合はnew String(str.getByte(「gbk」)で、「iso-8859-1」)で回転します.
js:
	var my_doaddMatrixOutNum = function(){
		Ext.lib.Ajax.defaultPostHeader += ";charset=utf8";
		Ext.Ajax.request({
			//     
			url : 'misliveaction.do?method=addMatrixOutNum',
			//      
			params : {
				matrixname :escape(Ext.get('matrixname').dom.value) ,
				outputnum :Ext.get('outputnum').dom.value ,
				desc:escape(Ext.get('m_desc').dom.value)
			},
			//      
			success : function(response, options) {
				ds.reload();
				winf.hide();
				//      json   
				var responseArray = Ext.util.JSON.decode(response.responseText);
				if (responseArray.success == true) {  
					Cookies.set('Allcard_userName', responseArray.user);

				} else {
				}
			}
		});
	}

action:


		bean.setName(MyString.gbktoIso(
					MyString.Unicode2GBK(name.replace("%", "\\"))
				));
		bean.setUrl_name(outputnum);
		bean.setDescription(MyString.gbktoIso(
				MyString.Unicode2GBK(desc.replace("%", "\\"))
		));
		
		dao.addMatrixOutNum(bean);

トランスコードクラス:
package com.c.elmer.util.string;

import java.io.UnsupportedEncodingException;

public class MyString {
	
	public static void main(String[] args) {
//		System.out.println(kbToMbOrG("123534",false));
//		System.out.println(0xdcbaa32  );
		System.out.println("hex:"+toHexUnicode("aA.\\u Windows 、Mac Chrome  ,Linux  Chrome          、        ,          ,  HTML5。  Chrome for Linux   Linux             ,           GTK  ,                             。Linux  Chrome               ,50          Chromium     。"));
		System.out.println(GBK2Unicode("      Windows 、Mac Chrome  ,Linux  Chrome          、        ,          ,  HTML5。  Chrome for Linux   Linux             ,           GTK  ,                             。Linux  Chrome               ,50          Chromium     。"));
		System.out.println(Unicode2GBK("\\ufeff\\u0061\\u0041\\u002e\\u005c\\u0075\\u548c\\u0057\\u0069\\u006e\\u0064\\u006f\\u0077\\u0073\\u7248\\u3001\\u004d\\u0061\\u0063\\u7248\\u0043\\u0068\\u0072\\u006f\\u006d\\u0065\\u4e00\\u6837\\uff0c\\u004c\\u0069\\u006e\\u0075\\u0078\\u7248\\u672c\\u0043\\u0068\\u0072\\u006f\\u006d\\u0065\\u7684\\u5f00\\u53d1\\u4e3b\\u8981\\u96c6\\u4e2d\\u5728\\u901f\\u5ea6\\u3001\\u7a33\\u5b9a\\u6027\\u548c\\u5b89\\u5168\\u6027\\u4e0a\\uff0c\\u8fd8\\u91c7\\u7528\\u4e86\\u591a\\u79cd\\u5f00\\u653e\\u6807\\u51c6\\uff0c\\u6bd4\\u5982\\u0048\\u0054\\u004d\\u004c\\u0035\\u3002\\u5f53\\u7136\\u0043\\u0068\\u0072\\u006f\\u006d\\u0065\\u0020\\u0066\\u006f\\u0072\\u0020\\u004c\\u0069\\u006e\\u0075\\u0078\\u4e5f\\u7ed3\\u5408\\u004c\\u0069\\u006e\\u0075\\u0078\\u73af\\u5883\\u5bf9\\u6d4f\\u89c8\\u5668\\u8fdb\\u884c\\u4e86\\u6027\\u80fd\\u63d0\\u5347\\uff0c\\u5176\\u4e2d\\u5305\\u62ec\\u9ad8\\u5ea6\\u6574\\u5408\\u4e86\\u672c\\u5730\\u0047\\u0054\\u004b\\u4e3b\\u9898\\uff0c\\u6b64\\u5916\\u4e5f\\u5bf9\\u591a\\u4e2a\\u53ef\\u4ee5\\u4e0e\\u64cd\\u4f5c\\u7cfb\\u7edf\\u7684\\u672c\\u5730\\u6027\\u8fdb\\u884c\\u534f\\u4f5c\\u7684\\u529f\\u80fd\\u8fdb\\u884c\\u4e86\\u8c03\\u6574\\u3002\\u004c\\u0069\\u006e\\u0075\\u0078\\u7248\\u672c\\u0043\\u0068\\u0072\\u006f\\u006d\\u0065\\u7684\\u5f00\\u53d1\\u83b7\\u5f97\\u4e86\\u5f00\\u6e90\\u793e\\u533a\\u7684\\u5927\\u529b\\u652f\\u6301\\uff0c\\u0035\\u0030\\u591a\\u4f4d\\u5f00\\u6e90\\u8d21\\u732e\\u8005\\u53c2\\u4e0e\\u4e86\\u0043\\u0068\\u0072\\u006f\\u006d\\u0069\\u0075\\u006d\\u7684\\u5f00\\u53d1\\u5de5\\u4f5c\\u3002"));
		System.out.println(Integer.parseInt("a",16));
		char a = 'a';
		
		System.out.println(Integer.toHexString((int)a));
		System.out.println( Integer.toHexString((int)" ".charAt(0))  );
		System.out.println("  UNICODE:U+554A"); 
		System.out.println(Integer.toHexString(" ".charAt(0) >> 8 & 0xff) 
				+" === " +Integer.toHexString(" ".charAt(0) & 0xff) 
				+"==="+Integer.toHexString(" ".charAt(0) & 0xff00)
				+"==="+Integer.toHexString(" ".charAt(0) | 0xff)
		); 
		String s = "abcdefghijklmnopqrstuvwxyz";
		System.out.println("=="+String.valueOf(2   *   s.charAt(3)));
//		Object o = new Object();
//		returnAddress 
	}
	
	
	
	/**
	 *     kb    MB GB
	 * @param kb
	 * @return
	 */
	public static String kbToMbOrG(String kb,boolean isb){
		String rtnStr = "";
		String type = "B";
		try{
			long kbI = (long)Double.parseDouble(kb);
			if(isb){
				if(kbI >= 1024){
					kbI = Math.round(kbI /1024);
					type = "KB";
				}				
			}
			if(kbI >= 1024){
				kbI = Math.round(kbI /1024);
				type = "MB";
			}
			if(kbI > 1024){
				kbI = Math.round(kbI /1024);
				type = "GB";				
			}
			if(kbI > 1024){
				kbI = Math.round(kbI /1024);
				type = "TB";				
			}
			rtnStr = kbI + "";
		}catch (Exception e) {
			e.printStackTrace();
		}
		return rtnStr+" "+type;
	}

	/**
	 * iso-8859-1  gbk
	 * @param iso
	 * @return
	 */
	public static String isotoGbk(String iso){
		String gbk = "";
		try {
			gbk = new String(iso.getBytes("iso-8859-1"),"gbk");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return gbk;
	}
	/**
	 * iso-8859-1  big5
	 * @param iso
	 * @return
	 */
	public static String iso2Big5(String iso){
		String gbk = "";
		try {
			gbk = new String(iso.getBytes("iso-8859-1"),"big5");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return gbk;
	}

	/**
	 * big5  iso-8859-1
	 * @param iso
	 * @return
	 */
	public static String Big52iso(String iso){
		String gbk = "";
		try {
			gbk = new String(iso.getBytes("big5"),"iso-8859-1");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return gbk;
	}
	
	/**
	 * gbk  iso-8859-1
	 * @param gbk
	 * @return
	 */
	public static String gbktoIso(String gbk){
		try {
			String iso = new String(gbk.getBytes("gbk"),"iso-8859-1");
			return iso;
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		
		return "";
	}
	
	/**
	 *        
	 * @param str
	 * @param sencd
	 * @param eencd
	 * @return
	 */
	public static String changeEncoding(String str,String sencd,String eencd){
		
		try {
			String rtnstr = new String(str.getBytes(sencd),eencd);
			return rtnstr;
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		
		return "";
		
	}

	public static String toHexUnicode(String str){
		StringBuffer result = new StringBuffer();
		result .append( "\\ufeff");
		for(int i=0;i<str.length();i++){
			char chr1 = (char)str.charAt(i);
			String hex_str = Integer.toHexString((int) chr1);
			if(hex_str.length() == 2){
				hex_str = "00" + hex_str;
			}
			result.append("\\u"+hex_str);
		}
		
		
		return result.toString();
	}
	
	/**
	 *    unicode
	 * @param str
	 * 
	 * @return   unicode  
	 */

	public static String GBK2Unicode(String str) {
		StringBuffer result = new StringBuffer();
		for (int i = 0; i < str.length(); i++) {
			char chr1 = (char) str.charAt(i);
			if (!PropertiesTranslater.isNeedConvert(chr1)) {
				result.append(chr1);
				continue;

			}
			result.append("\\u" + Integer.toHexString((int) chr1));
		}

		return result.toString();

	}

	/**
	 * 
	 * unicode   
	 * @param str
	 * 
	 * @return   
	 * 
	 */

	public static String Unicode2GBK(String dataStr) {
                  if(dataStr.indexOf("\\u") == -1 ){
			return dataStr;
		}
		int index = 0;

		StringBuffer buffer = new StringBuffer();
		while (index < dataStr.length()) {

			if (!"\\u".equals(dataStr.substring(index, index + 2))) {
				buffer.append(dataStr.charAt(index));
				index++;
				continue;
			}
			String charStr = "";

			charStr = dataStr.substring(index + 2, index + 6);
			
			char letter = (char) Integer.parseInt(charStr, 16);
			
			buffer.append(letter);

			index += 6;
		}
		return buffer.toString();

	}

	public static boolean isNeedConvert(char para) {
		return ((para & (0x00FF)) != para);
	}
	

	/**
	 * gbk  utf-8
	 * @param gbk
	 * @return
	 */
    public String gbk2utf8(String gbk){   
         String utf8 = "";   
         try {   
             utf8 = new String(gbk2utf8_c(gbk),"UTF-8");   
         } catch (UnsupportedEncodingException e) {   
             e.printStackTrace();   
         }   
         return utf8;   
     }   

     public byte[] gbk2utf8_c(String chenese) {   
         char c[] = chenese.toCharArray();   
         byte[] fullByte = new byte[3 * c.length];   
         for (int i = 0; i < c.length; i++) {   
             int m = (int) c[i];   
             String word = Integer.toBinaryString(m);   

             StringBuffer sb = new StringBuffer();   
             int len = 16 - word.length();   
             for (int j = 0; j < len; j++) {   
                 sb.append("0");   
             }   
             sb.append(word);   
             sb.insert(0, "1110");   
             sb.insert(8, "10");   
             sb.insert(16, "10");   

             String s1 = sb.substring(0, 8);   
             String s2 = sb.substring(8, 16);   
             String s3 = sb.substring(16);   

             byte b0 = Integer.valueOf(s1, 2).byteValue();   
             byte b1 = Integer.valueOf(s2, 2).byteValue();   
             byte b2 = Integer.valueOf(s3, 2).byteValue();   
             byte[] bf = new byte[3];   
             bf[0] = b0;   
             fullByte[i * 3] = bf[0];   
             bf[1] = b1;   
             fullByte[i * 3 + 1] = bf[1];   
             bf[2] = b2;   
             fullByte[i * 3 + 2] = bf[2];   

         }   
         return fullByte;   
     }   

	


}

絶対実行可能