随笔-55  评论-208  文章-0  trackbacks-0
上次提出了一个ajax下载中文编码问题,这次有遇到了ajax的上传中文乱麻问题
继续bs各种编码:)

遇到问题:在使用xhr发出请求时,请求的url中参数带中文,这时在后台解析request得不到中文
如:
request = new ActiveXObject("Microsoft.XMLHTTP");
request.open("GET", url);中我的url需要带中文参数
直接加参数是在后台解析不出来的,所以我使用方法对url进行编码转换
request = new ActiveXObject("Microsoft.XMLHTTP");
request.open(
"GET", uriFunc());

//省略了对象定义
//
..


function uriFunc() {
    
var separator = "?";
    
if (uri.indexOf("?">= 0)
        separator 
= "&";        
    
return uri + separator + paramName + "=" + escape(inputField.value,'utf-8');
  }
然后就要在后台进行解码了
以为没法直接从request中取到编码字符串,所以使用方法先取出url,然后再取出url的参数进行解码
取出url,以及取参数方法
String exfirstword = getUrlParameter(unescape(request.getQueryString()),"excludeuser"); 
 

 
public String getUrlParameter(String str,String targetstr){
          
          String[] stringarray 
= str.split("&")  ;
            
            
for(int i=0;i<stringarray.length;i++){
               
if(stringarray[i].startsWith(targetstr)){
                   stringarray
=stringarray[i].split("=");    
                   
break;
               }
            }
            
return stringarray[1];
      }

 
//解码方法
 
//以下是对js的escape进行解码
      private final static byte[] val = { 0x3F0x3F0x3F0x3F0x3F0x3F,
          
0x3F0x3F0x3F0x3F0x3F0x3F0x3F0x3F0x3F0x3F0x3F,
          
0x3F0x3F0x3F0x3F0x3F0x3F0x3F0x3F0x3F0x3F0x3F,
          
0x3F0x3F0x3F0x3F0x3F0x3F0x3F0x3F0x3F0x3F0x3F,
          
0x3F0x3F0x3F0x3F0x3F0x3F0x3F0x3F0x3F0x000x01,
          
0x020x030x040x050x060x070x080x090x3F0x3F0x3F,
          
0x3F0x3F0x3F0x3F0x0A0x0B0x0C0x0D0x0E0x0F0x3F,
          
0x3F0x3F0x3F0x3F0x3F0x3F0x3F0x3F0x3F0x3F0x3F,
          
0x3F0x3F0x3F0x3F0x3F0x3F0x3F0x3F0x3F0x3F0x3F,
          
0x3F0x3F0x3F0x0A0x0B0x0C0x0D0x0E0x0F0x3F0x3F,
          
0x3F0x3F0x3F0x3F0x3F0x3F0x3F0x3F0x3F0x3F0x3F,
          
0x3F0x3F0x3F0x3F0x3F0x3F0x3F0x3F0x3F0x3F0x3F,
          
0x3F0x3F0x3F0x3F0x3F0x3F0x3F0x3F0x3F0x3F0x3F,
          
0x3F0x3F0x3F0x3F0x3F0x3F0x3F0x3F0x3F0x3F0x3F,
          
0x3F0x3F0x3F0x3F0x3F0x3F0x3F0x3F0x3F0x3F0x3F,
          
0x3F0x3F0x3F0x3F0x3F0x3F0x3F0x3F0x3F0x3F0x3F,
          
0x3F0x3F0x3F0x3F0x3F0x3F0x3F0x3F0x3F0x3F0x3F,
          
0x3F0x3F0x3F0x3F0x3F0x3F0x3F0x3F0x3F0x3F0x3F,
          
0x3F0x3F0x3F0x3F0x3F0x3F0x3F0x3F0x3F0x3F0x3F,
          
0x3F0x3F0x3F0x3F0x3F0x3F0x3F0x3F0x3F0x3F0x3F,
          
0x3F0x3F0x3F0x3F0x3F0x3F0x3F0x3F0x3F0x3F0x3F,
          
0x3F0x3F0x3F0x3F0x3F0x3F0x3F0x3F0x3F0x3F0x3F,
          
0x3F0x3F0x3F0x3F0x3F0x3F0x3F0x3F0x3F0x3F0x3F,
          
0x3F0x3F0x3F0x3F0x3F0x3F0x3F0x3F };

     
      
/**
       * 解码 说明:本方法保证 不论参数s是否经过escape()编码,均能得到正确的“解码”结果
       *
       * 
@param s
       * 
@return
       
*/
      
public static String unescape(String s) {
      StringBuffer sbuf 
= new StringBuffer();
      
int i = 0;
      
int len = s.length();
      
while (i < len) {
      
int ch = s.charAt(i);
      
if ('A' <= ch && ch <= 'Z') { // 'A'..'Z' : as it was
      sbuf.append((char) ch);
      } 
else if ('a' <= ch && ch <= 'z') { // 'a'..'z' : as it was
      sbuf.append((char) ch);
      } 
else if ('0' <= ch && ch <= '9') { // '0'..'9' : as it was
      sbuf.append((char) ch);
      } 
else if (ch == '-' || ch == '_' // unreserved : as it was
      || ch == '.' || ch == '!' || ch == '~' || ch == '*'
      
|| ch == '\'' || ch == '(' || ch == ')') {
      sbuf.append((char) ch);
      } 
else if (ch == '%') {
      
int cint = 0;
      
if ('u' != s.charAt(i + 1)) { // %XX : map to ascii(XX)
      cint = (cint << 4| val[s.charAt(i + 1)];
      cint 
= (cint << 4| val[s.charAt(i + 2)];
      i 
+= 2;
      } 
else { // %uXXXX : map to unicode(XXXX)
      cint = (cint << 4| val[s.charAt(i + 2)];
      cint 
= (cint << 4| val[s.charAt(i + 3)];
      cint 
= (cint << 4| val[s.charAt(i + 4)];
      cint 
= (cint << 4| val[s.charAt(i + 5)];
      i 
+= 5;
      }
      sbuf.append((
char) cint);
      } 
else { // 对应的字符未经过编码
      sbuf.append((char) ch);
      }
      i
++;
      }
      
return sbuf.toString();
      }

这时候得到的exfirstword 就是正确的中文了
问题解决

posted on 2006-05-25 20:49 rocket 阅读(1952) 评论(3)  编辑  收藏

评论:
# re: ajax中文上传编码问题 2006-05-31 12:33 | arlen
感谢. 可是为什么直接用中文就不可以呢??? 我得到得中文是gbk编码的, 但是没法转成utf-8. 觉得跟很多因素有关系  回复  更多评论
  
# re: ajax中文上传编码问题 2006-06-01 09:54 | rocket
@arlen

是这样的,在使用xhr发出请求时,实际上是向后台发送了一个http的包,只不过这个http的包是封装的xmlhttprequest,而xmlhttprequest对中文的编码方式是gb2312的,这样由于页面用的是utf-8编码,所以在后台就会出现问题了。
所以如果你的页面是gbK或者gb2312编码的,这样在用xmlhttprequest就没有这么多麻烦了  回复  更多评论
  
# re: ajax中文上传编码问题 2007-01-17 18:06 | nova
谢谢你,帮了我一个大忙。  回复  更多评论
  

只有注册用户登录后才能发表评论。


网站导航: