中文乱码 URLEncode之后,后台获取仍是乱码问题详解

来源:互联网 发布:mac连不上wifi 编辑:程序博客网 时间:2024/06/14 08:43
在java中获取到参数的时候,这时候默认使用的是iso8859-1进行解码的,那么就再使用URLEncode的encode方法对其进行编码一次,编码格式使用iso8859-1,这样我们就获得最初使用utf-8编码之后的字符了,接着再使用URLDecoder的decode方法对其进行解码,解码的时候第一个参数为编码之后的字符,第二个参数指定为utf-8,和编码时一样的码表就行,这样就获得正确的参数了。如下代码:

//获取使用iso8859-1解码之后的字符串  
     projectname = request.getParameter("projectname");  
     //对使用iso8859-1解码后的字符串  使用iso8859-1进行编码  获得最初使用utf-8进行编码之后的字符串  
     projectname = URLEncoder.encode(projectname, "iso8859-1");  
     //对获得utf-8编码之后的字符串使用URLDecoder的decode方法解码  指定解码方式为utf-8  
     projectname = URLDecoder.decode(projectname, "utf-8");

过程 原始值--(UTF-8)---(iso8859-1) 接收之后是iso8859-1
(iso8859-1)-逆编码(utf-8)--解码原始值
------------------------------
window.location.href = encodeURI('/ipms/excel/cluesExcelExport.action?'
                            +'&flagnameen='+flagnameen
                            +'&projectid='+projectid
                            +'&projecttype='+projectTypeValue
                            +'&username='+encodeURI(username)
                            +'&cusname='+encodeURI(cusname)
                            +'&cusMngname='+encodeURI(cusMngname)
                            +'&productNamectype='+productNamectype
                            //+'&producttype_id='+Ext.get('producttype_id').dom.value
                            +'&signstate=0');

 String username = request.getParameter("username") == null ? "" :URLDecoder.decode(request.getParameter("username"),"utf-8" );
        
        //add by sunali ---2014-06-06--增加查询条件--start
        String cusname = request.getParameter("cusname")==null?"":URLDecoder.decode(request.getParameter("cusname"), "utf-8");
        String cusMngname = request.getParameter("cusMngname")==null?"":URLDecoder.decode(request.getParameter("cusMngname"), "utf-8");



原创粉丝点击