Java开发WAP中文问题解决方案

 
< %@   page    language ="java" % >    
  
< %@   page    contentType ="text/vnd.wap.wml"    pageEncoding ="UTF-8" % >    
  
<? xml   version="1.0"   encoding="UTF-8" ?>    
  
<! DOCTYPE   wml   PUBLIC   "-//WAPFORUM//DTD   WML   1.1//EN"   "http://www.wapforum.org/DTD/wml_1.1.xml" >    
  
< wml >    
  
< card >    
  
</ card >    
  
</ wml >    

 

对于输出页面上的中文,写一个类来转换成UTF,这样就解决了在不同平台显示中文的问题,而且移动的网关也是utf-8的。

 

package  cn._5znb.j2ee.tag.base;

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

/** */ /**
 * 转化字符的类
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2006</p>
 * <p>Company: sinotec</p>
 * 
@author 唐韧
 * 
@version 1.0
 
*/

public   class  ConvertString
... {
    
private static ConvertString cs = null ;
    
private ConvertString ()
    
...{
    }


    
public static ConvertString getInstance ()
    
...{
        
if ( cs == null )
        
...{
            cs 
= new ConvertString () ;
        }

        
return cs ;
    }


    
public static String IsoConvertGb ( String strIn )
    
...{
        String strOut 
= null ;
        
if ( strIn == null || ( strIn.trim () ).equals ( "" ) )
        
...{
            
return strIn ;
        }

        
try
        
...{
            
byte[] b = strIn.getBytes ( "ISO8859_1" ) ;
            strOut 
= new String ( b , "GBK" ) ;
        }

        
catch ( Exception e )
        
...{}
        
return strOut ;
    }


    
public static String ConvertString ( String s )
    
...{
        StringBuffer ustring 
= new StringBuffer () ;
        s 
= IsoConvertGb ( s ) ;
        
for ( int i = 0 ; i < s.length () ; i++ )
        
...{
            Character c 
= new Character ( s.charAt ( i ) ) ;
            
if ( c.toString ().getBytes ().length == 1 )
            
...{
                ustring.append ( c.charValue () ) ;
            }

            
else
            
...{
                
int a = ( int ) c.charValue () ;
                String temp 
= new String ( "&#x" + Integer.toHexString ( a ) +
                                           
";" ) ;
                ustring.append ( temp ) ;
            }

        }

        
return ustring.toString () ;
    }


    
public String getStr ( String str )
    
...{
        
try
        
...{
            String temp_p 
= str ;
            
byte[] temp_t = temp_p.getBytes ( "ISO8859-1" ) ;
            String temp 
= new String ( temp_t ) ;
            
return temp ;
        }

        
catch ( Exception e )
        
...{

        }

        
return "null" ;
    }


    
static public String convertUTF8String2Unicode ( String instr )
        
throws IOException
    
...{
        
int charindex = instr.length () ;
        
int actualvalue ;
        
int inputvalue ;
        StringBuffer sbtemp 
= new StringBuffer () ;

        
for ( int i = 0 ; i < charindex ; )
        
...{
            actualvalue 
= -1 ;
            inputvalue 
= instr.charAt ( i++ ) ;

            inputvalue 
&= 0xff ;

            
if ( ( inputvalue & 0x80 ) == 0 )
            
...{
                actualvalue 
= inputvalue ;
            }

            
else if ( ( inputvalue & 0xF8 ) == 0xF0 )
            
...{
                actualvalue 
= ( inputvalue & 0x1f ) << 18 ;

                
int nextByte = instr.charAt ( i++ ) & 0xff ;
                
if ( ( nextByte & 0xC0 ) != 0x80 )
                
...{
                    
throw new IOException ( "Invalid UTF-8 format" ) ;
                }

                actualvalue 
+= ( nextByte & 0x3F ) << 12 ;

                nextByte 
= instr.charAt ( i++ ) & 0xff ;
                
if ( ( nextByte & 0xC0 ) != 0x80 )
                
...{
                    
throw new IOException ( "Invalid UTF-8 format" ) ;
                }

                actualvalue 
+= ( nextByte & 0x3F ) << 6 ;

                nextByte 
= instr.charAt ( i++ ) & 0xff ;
                
if ( ( nextByte & 0xC0 ) != 0x80 )
                
...{
                    
throw new IOException ( "Invalid UTF-8 format" ) ;
                }

OutliningIndi
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值