utf-8 转为 gb2312 和 smartupload上传文件并附text域的值的汉字解决办法(注意是:WAP,XHTML或XML中)

1. utf-8 转为 gb2312
 <%@page contentType="text/html; charset=gb2312"%><%@page session="true" %>    
<html>
<head>
    <meta http-equiv="Cache-Control" content="max-age=0" forua="true"/>
    <meta http-equiv="Cache-control" content="no-cache" forua="true"/>
</head>    
<body>
        <p>            
            <%
              String str="你好莱坞";
              out.print(enCode(str));
              out.print("<br/>"+deCode(enCode(str)));
            %>
            <br/>
        </p>
</body>
</html>


<%!
String enCode(String str)                                    
      {                                                        
        if(str==null)return "";                                
        String hs="";                                          
                                                             
        try                                                    
        {                                                      
          byte b[]=str.getBytes("UTF-16");                     
          //System.out.println(byte2hex(b));                   
          for (int n=0;n<b.length;n++)                         
          {                                                    
              str=(java.lang.Integer.toHexString(b[n] & 0XFF));
              if (str.length()==1)                             
                hs=hs+"0"+str;                                 
              else                                             
                hs=hs+str;                                     
              if (n!=b.length-1)hs=hs+"";                      
          }                                                    
                                                               
          str= hs.toUpperCase().substring(4);                  
          //System.out.println(str);                           
          char[] chs=str.toCharArray();                        
          str="";                                              
          for(int i=0;i<chs.length;i=i+4)                      
          {                                                    
            str+="&#x"+chs[i]+chs[i+1]+chs[i+2]+chs[i+3]+";";  
          }                                                    
                                                               
        }                                                      
        catch(Exception e)                                     
        {                                                      
          System.out.print(e.getMessage());                    
        }                                                      
        return str;                                            
      }             


String deCode(String str)                                                 
    {                                                                                                           
        if (str == null)                                                
        {                                                               
            return "";                                                    
        }                                                               
        String temp = "";                                               
        String return_temp = "";                                        
        try                                                             
        {                                                               
            int str_len = str.length() / 7;                               
            for (int i = 1; i <= str_len; i++)                            
            {                                                             
                temp += str.substring(3 * i + 5 * (i - 1), 7 * i + (i - 1));
            }                                                             
            temp = "FEFF" + temp;                                         
            temp = temp.toLowerCase();                                    
            int temp_len = temp.length() / 2;                             
            byte mm[] = new byte[temp_len];                               
            String ss[] = new String[temp_len];                           
            for (int i = 0; i < temp_len; i++)                            
            {                                                             
                String qq = temp.substring(i * 2, i * 2 + 1);               
                if (!qq.equals("0"))                                        
                {                                                           
                    ss[i] = temp.substring(i * 2, i * 2 + 2);                 
                }                                                           
                else                                                        
                {                                                           
                    ss[i] = temp.substring(i * 2 + 1, i * 2 + 2);             
                }                                                           
                mm[i] = (byte) Integer.parseInt(ss[i], 16);                 
                System.out.println(mm[i]);                                  
            }                                                             
            return_temp = new String(mm, "UTF-16");                       
        }                                                               
        catch (Exception e)                                             
        {                                                               
            System.out.print(e.getMessage());                             
        }                                                               
        return return_temp;                                             
    }                                        
    
    %>               
2.smartupload上传文件并附text域的值的汉字解决办法(注意是:WAP,XHTML或XML中,HTML中则不要转换)
<%@ page contentType="application/vnd.wap.xhtml+xml;charset=UTF-8"  import="javax.servlet.*,com.framework.*,com.jspsmart.upload.*, java.util.*,java.lang.*"%><?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE html PUBLIC "-//WAPFORUM//DTD XHTML Mobile 1.0//EN" "http://www.wapforum.org/DTD/xhtml-mobile10.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Wap2.0&#x4e0a;&#x4f20;&#x6587;&#x4ef6;&#x5e76;&#x540c;&#x65f6;&#x53d1;&#x8868;&#x6587;&#x7ae0;</title>
</head>
<body>
<p>
<%
// 初始化
final int MaxFileSize=1024000*4;        //上传文件大小限制
String ErrMsg=null;    //错误信息
boolean ERR=false;     //错误标志
com.jspsmart.upload.SmartUpload mySmartUpload=new com.jspsmart.upload.SmartUpload();
mySmartUpload.initialize(pageContext);

// 上传文件
mySmartUpload.upload();

String authorName=mySmartUpload.getRequest().getParameter("authorName");
out.println(".authorName="+new String(java.net.URLDecoder.decode(_authorName,"utf-8").getBytes("gbk") ));

............

%>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值