<%@ 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的。
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" ) ;
}
actualvalue += ( nextByte & 0x3F ) ;
}
else if ( ( inputvalue & 0xF0 ) == 0xE0 )
...{
actualvalue = ( inputvalue & 0x1f ) << 12 ;
int 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" ) ;
}
actualvalue += ( nextByte & 0x3F ) ;
}
else if ( ( inputvalue & 0xE0 ) == 0xC0 )
...{
actualvalue = ( inputvalue & 0x1f ) << 6 ;
int nextByte = instr.charAt ( i++ ) & 0xff ;
if ( ( nextByte & 0xC0 ) != 0x80 )
...{
throw new IOException ( "Invalid UTF-8 format" ) ;
}
actualvalue += ( nextByte & 0x3F ) ;
}
sbtemp.append ( ( char ) actualvalue ) ;
}
return sbtemp.toString () ;
}
public static byte[] convertUnicode2UTF8Byte ( String instr )
...{
int len = instr.length () ;
byte[] abyte = new byte[ len << 2 ] ;
int j = 0 ;
for ( int i = 0 ; i < len ; i++ )
...{
char c = instr.charAt ( i ) ;
if ( c < 0x80 )
...{
abyte[ j++ ] = ( byte ) c ;
}
else if ( c < 0x0800 )
...{
abyte[ j++ ] = ( byte ) ( ( ( c >> 6 ) & 0x1F ) | 0xC0 ) ;
abyte[ j++ ] = ( byte ) ( ( c & 0x3F ) | 0x80 ) ;
}
else if ( c < 0x010000 )
...{
abyte[ j++ ] = ( byte ) ( ( ( c >> 12 ) & 0x0F ) | 0xE0 ) ;
abyte[ j++ ] = ( byte ) ( ( ( c >> 6 ) & 0x3F ) | 0x80 ) ;
abyte[ j++ ] = ( byte ) ( ( c & 0x3F ) | 0x80 ) ;
}
else if ( c < 0x200000 )
...{
abyte[ j++ ] = ( byte ) ( ( ( c >> 18 ) & 0x07 ) | 0xF8 ) ;
abyte[ j++ ] = ( byte ) ( ( ( c >> 12 ) & 0x3F ) | 0x80 ) ;
abyte[ j++ ] = ( byte ) ( ( ( c >> 6 ) & 0x3F ) | 0x80 ) ;
abyte[ j++ ] = ( byte ) ( ( c & 0x3F ) | 0x80 ) ;
}
}
byte[] retbyte = new byte[ j ] ;
for ( int i = 0 ; i < j ; i++ )
...{
retbyte[ i ] = abyte[ i ] ;
}
return retbyte ;
}
public static String ISO106462Unicode ( byte[] myByte )
...{
String result = new String ( "" ) ;
StringBuffer sb = new StringBuffer ( "" ) ;
try
...{
int len = myByte.length ;
for ( int i = 0 ; i < len ; i = i + 2 )
...{
byte hiByte = myByte[ i ] ;
byte loByte = myByte[ i + 1 ] ;
int ch = ( int ) hiByte << 8 ;
ch = ch & 0xff00 ;
ch += ( int ) loByte & 0xff ;
sb.append ( ( char ) ch ) ;
}
result = new String ( sb.toString () ) ;
}
catch ( Exception e )
...{
System.out.println ( "Encoding Error" ) ;
}
return result ;
}
public static byte[] Unicode2Byte ( String s )
...{
int len = s.length () ;
byte abyte[] = new byte[ len << 1 ] ;
int j = 0 ;
for ( int i = 0 ; i < len ; i++ )
...{
char c = s.charAt ( i ) ;
abyte[ j++ ] = ( byte ) ( c & 0xff ) ;
abyte[ j++ ] = ( byte ) ( c >> 8 ) ;
}
return abyte ;
}
private StringBuffer decodeUnicode ( final String dataStr )
...{
int start = 0 ;
int end = 0 ;
final StringBuffer buffer = new StringBuffer () ;
while ( start > -1 )
...{
end = dataStr.indexOf ( "/u" , start + 2 ) ;
String charStr = "" ;
if ( end == -1 )
...{
charStr = dataStr.substring ( start + 2 , dataStr.length () ) ;
}
else
...{
charStr = dataStr.substring ( start + 2 , end ) ;
}
char letter = ( char ) Integer.parseInt ( charStr , 16 ) ;
buffer.append ( new Character ( letter ).toString () ) ;
start = end ;
}
return buffer ;
}
static public String convert ( String instr )
throws IOException
...{
try
...{
String temp = new String ( convertUnicode2UTF8Byte ( instr ) ,
"UTF-8" ) ;
String result = new String ( convertUTF8String2Unicode ( temp ).
getBytes ( "GBK" ) , "ISO-8859-1" ) ;
return result ;
}
catch ( Exception e )
...{
}
return "null" ;
}
public static void main ( String[] args )
throws UnsupportedEncodingException , UnsupportedEncodingException ,
IOException
...{
ConvertString cs = new ConvertString();
//System.out.print(cs.ConvertString("唐韌"));
}
}