用socket 传中文字串 乱码问题。 如何传中文我不太清楚但是可以用Base64把字符串转换成只包含英文字符的字符串编码,传送后再解码 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 ps.println(new String(buf, "gb23112")); /* 开发者:王飞平 [email protected] Base64 字符表 码值 字符 码值 字符 码值 字符 码值 字符 0 A 17 R 34 i 51 z 1 B 18 S 35 j 52 0 2 C 19 T 36 k 53 1 3 D 20 U 37 l 54 2 4 E 21 V 38 m 55 3 5 F 22 W 39 n 56 4 6 G 23 X 40 o 57 5 7 H 24 Y 41 p 58 6 8 I 25 Z 42 q 59 7 9 J 26 a 43 r 60 8 10 K 27 b 44 s 61 9 11 L 28 c 45 t 62 + 12 M 29 d 46 u 63 / 13 N 30 e 47 v 14 O 31 f 48 w (pad) = 15 P 32 g 49 x 16 Q 33 h 50 y*/import java.io.*;public class Base64{ public static char BASETABLE[]={'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','0','1','2','3','4','5','6','7','8','9','+','/','='}; public static String encode(String text){ /*编码算法 1.将数据按3个字节一组分成数块; 2.每块将3个8位的数据转换成4个6位数据段; 11111111 00000000 11111111 ---- 111111 110000 000011 111111 3.根据Base64字符表得到4个6位数据段对应的字符; 4.如果最后一块只有两个字节,则添加两个0位,转换成对应Base64字符表的三个字符,并在结尾添一个'='字符; 如果最后一块只有一个字节,则添加四个0位,转换成对应Base64字符表的两个字符,并在结尾添两个'='字符。 */ StringBuffer code=new StringBuffer(); int textLength=text.getBytes().length; int blockCount=(int)textLength/3; int ch, bits, textChars[]=new int[textLength]; byte[] textBytes=text.getBytes(); for(int i=0;i<textLength;i++) textChars[i]=textBytes[i]>=0 ? textBytes[i] : 128-textBytes[i]; for(int i=0;i<blockCount;i++){ ch=(int)textChars[0+i*3]; ch=ch>>>2; code.append(BASETABLE[ch]); bits=(int)textChars[0+i*3]-ch*4; ch=(int)textChars[1+i*3]; ch=ch>>>4; code.append(BASETABLE[ch+bits*16]); bits=(int)textChars[1+i*3]-ch*16; ch=(int)textChars[2+i*3]; ch=ch>>>6; code.append(BASETABLE[ch+bits*4]); bits=(int)textChars[2+i*3]-ch*64; code.append(BASETABLE[bits]); } if((textLength % 3)!=0) { ch=(int)textChars[blockCount*3]; ch=ch>>>2; code.append(BASETABLE[ch]); bits=(int)textChars[blockCount*3]-ch*4; switch(textLength % 3){ case 1: code.append(BASETABLE[bits*16]); code.append(BASETABLE[64]); code.append(BASETABLE[64]); break; case 2: ch=(int)textChars[1+blockCount*3]; ch=ch>>>4; code.append(BASETABLE[ch+bits*16]); bits=(int)textChars[1+blockCount*3]-ch*16; code.append(BASETABLE[bits*4]); code.append(BASETABLE[64]); break; } } return code.toString(); } public static String decode(String code){ /*解码算法 1.将数据按4个字节一组分成数块; 2.每块将4个字符去掉最高两位并转换成3个8位的数据段; 注意:数据块中的字符取值不是ASCII集的值,而是Base64字符表中相对应的索引值! 00 111111 00 110000 00 000011 00 111111 ---- 11111111 00000000 11111111 3.根据ASCII字符集得到3个8位数据段对应的字符; 4.如果最后一块只有两个'=',去掉两个'=',并去掉最低两位,转换成对应ASSCII字符集的两个字符; 如果最后一块只有一个'=',去掉'=',并去掉最低四位,转换成对应ASSCII字符集的一个字符。 */ int codeLength=code.length(); int blockCount=(int)codeLength/4; char[] codeChars=new char[codeLength]; int ch, bits; code.getChars(0, codeLength, codeChars, 0); byte[] textBytes; if(codeChars[codeLength-2]=='=') textBytes=new byte[codeLength/4*3-2]; else if(codeChars[codeLength-1]=='=') textBytes=new byte[codeLength/4*3-1]; else textBytes=new byte[codeLength/4*3]; for(int i=0;i<blockCount;i++){ bits=indexOfBase64Table(codeChars[1+i*4])>>>4; ch=indexOfBase64Table(codeChars[0+i*4]); textBytes[0+i*3]=(byte)(ch>=32 ? (32-ch)*4-bits : ch*4+bits); if(codeChars[2+i*4]!='='){ ch=indexOfBase64Table(codeChars[1+i*4])-bits*16; bits=indexOfBase64Table(codeChars[2+i*4])>>>2; if(ch>=8) ch=(8-ch)*16-bits; else ch=ch*16+bits; textBytes[1+i*3]=(byte)ch; if(codeChars[3+i*4]!='='){ ch=indexOfBase64Table(codeChars[2+i*4])-bits*4; if(ch>=2) ch=(2-ch)*64-indexOfBase64Table(codeChars[3+i*4]); else ch=ch*64+indexOfBase64Table(codeChars[3+i*4]); textBytes[2+i*3]=(byte)ch; } } } String text=null; try{ text=new String(textBytes,"gb2312"); }catch(UnsupportedEncodingException e){} return text; } private static int indexOfBase64Table(char ch){ for(int i=0;i<64;i++) if(ch==BASETABLE[i]) return i; return -1; }} 试试看,找个支持编码的流对象,建你这样写://输入:BufferedReader in=new BufferedReader( new InputStreamReader( socket.getInputStream(),"GBK"));输出:PrintWrite out=new PrintWrite( new BufferedWriter( new OutputStreamWriter( socket.getOutputStream(),"GBK")),true);out是你的ps,in是你的dis,socket是一个Socket对象。 多谢junnef(皮皮) ,万分感谢!!!另外还有一个问题请教, readln 是读取一行内容,如果有多行内容怎么读取?万望给予解答,谢谢了!!! 可以:写个循环,不停的readLine;也可以直接read()读固定字节; 可以把中文字符串转成iso8859_1传输,然后接受端再从iso8859_1转为gb2312 局域网多线程下载的问题! 关于JVM的64bit和32bit经典问题,大家讨论下,最好有人试验过 MySql数据库乱码问题 寻早JTree高手————在JTree的节点上点右键弹出菜单,解决问题马上给分!在线等 对Java1.5(5.0)的使用情况统计! 流结束时in.read()返回-1,请问如何判断流结束? 求助,关于Javamail的问题! IllegalBlockSizeException 高手请关注!! 100分!只能给这么多!! 在线等候。 与16进制进行&运算 帮忙看看下面两句代码作用是什么? 如何向Graphics上画一个点?? 谁有java中垃圾收集的精彩文章,谢谢。
开发者:王飞平
[email protected]
Base64 字符表
码值 字符 码值 字符 码值 字符 码值 字符
0 A 17 R 34 i 51 z
1 B 18 S 35 j 52 0
2 C 19 T 36 k 53 1
3 D 20 U 37 l 54 2
4 E 21 V 38 m 55 3
5 F 22 W 39 n 56 4
6 G 23 X 40 o 57 5
7 H 24 Y 41 p 58 6
8 I 25 Z 42 q 59 7
9 J 26 a 43 r 60 8
10 K 27 b 44 s 61 9
11 L 28 c 45 t 62 +
12 M 29 d 46 u 63 /
13 N 30 e 47 v
14 O 31 f 48 w (pad) =
15 P 32 g 49 x
16 Q 33 h 50 y
*/
import java.io.*;public class Base64{
public static char BASETABLE[]={'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','0','1','2','3','4','5','6','7','8','9','+','/','='}; public static String encode(String text){
/*编码算法
1.将数据按3个字节一组分成数块;
2.每块将3个8位的数据转换成4个6位数据段;
11111111 00000000 11111111 ---- 111111 110000 000011 111111
3.根据Base64字符表得到4个6位数据段对应的字符;
4.如果最后一块只有两个字节,则添加两个0位,转换成对应Base64字符表的三个字符,并在结尾添一个'='字符;
如果最后一块只有一个字节,则添加四个0位,转换成对应Base64字符表的两个字符,并在结尾添两个'='字符。
*/
StringBuffer code=new StringBuffer();
int textLength=text.getBytes().length;
int blockCount=(int)textLength/3;
int ch, bits, textChars[]=new int[textLength];
byte[] textBytes=text.getBytes(); for(int i=0;i<textLength;i++)
textChars[i]=textBytes[i]>=0 ? textBytes[i] : 128-textBytes[i]; for(int i=0;i<blockCount;i++){
ch=(int)textChars[0+i*3];
ch=ch>>>2;
code.append(BASETABLE[ch]);
bits=(int)textChars[0+i*3]-ch*4;
ch=(int)textChars[1+i*3];
ch=ch>>>4;
code.append(BASETABLE[ch+bits*16]);
bits=(int)textChars[1+i*3]-ch*16;
ch=(int)textChars[2+i*3];
ch=ch>>>6;
code.append(BASETABLE[ch+bits*4]);
bits=(int)textChars[2+i*3]-ch*64;
code.append(BASETABLE[bits]);
}
if((textLength % 3)!=0)
{ ch=(int)textChars[blockCount*3];
ch=ch>>>2;
code.append(BASETABLE[ch]);
bits=(int)textChars[blockCount*3]-ch*4;
switch(textLength % 3){
case 1: code.append(BASETABLE[bits*16]);
code.append(BASETABLE[64]);
code.append(BASETABLE[64]);
break;
case 2: ch=(int)textChars[1+blockCount*3];
ch=ch>>>4;
code.append(BASETABLE[ch+bits*16]);
bits=(int)textChars[1+blockCount*3]-ch*16;
code.append(BASETABLE[bits*4]);
code.append(BASETABLE[64]);
break;
}
} return code.toString();
} public static String decode(String code){
/*解码算法
1.将数据按4个字节一组分成数块;
2.每块将4个字符去掉最高两位并转换成3个8位的数据段;
注意:数据块中的字符取值不是ASCII集的值,而是Base64字符表中相对应的索引值!
00 111111 00 110000 00 000011 00 111111 ---- 11111111 00000000 11111111
3.根据ASCII字符集得到3个8位数据段对应的字符;
4.如果最后一块只有两个'=',去掉两个'=',并去掉最低两位,转换成对应ASSCII字符集的两个字符;
如果最后一块只有一个'=',去掉'=',并去掉最低四位,转换成对应ASSCII字符集的一个字符。
*/
int codeLength=code.length();
int blockCount=(int)codeLength/4;
char[] codeChars=new char[codeLength];
int ch, bits;
code.getChars(0, codeLength, codeChars, 0); byte[] textBytes;
if(codeChars[codeLength-2]=='=') textBytes=new byte[codeLength/4*3-2];
else if(codeChars[codeLength-1]=='=') textBytes=new byte[codeLength/4*3-1];
else textBytes=new byte[codeLength/4*3];
for(int i=0;i<blockCount;i++){
bits=indexOfBase64Table(codeChars[1+i*4])>>>4;
ch=indexOfBase64Table(codeChars[0+i*4]);
textBytes[0+i*3]=(byte)(ch>=32 ? (32-ch)*4-bits : ch*4+bits);
if(codeChars[2+i*4]!='='){
ch=indexOfBase64Table(codeChars[1+i*4])-bits*16;
bits=indexOfBase64Table(codeChars[2+i*4])>>>2;
if(ch>=8) ch=(8-ch)*16-bits;
else ch=ch*16+bits;
textBytes[1+i*3]=(byte)ch;
if(codeChars[3+i*4]!='='){
ch=indexOfBase64Table(codeChars[2+i*4])-bits*4;
if(ch>=2) ch=(2-ch)*64-indexOfBase64Table(codeChars[3+i*4]);
else ch=ch*64+indexOfBase64Table(codeChars[3+i*4]);
textBytes[2+i*3]=(byte)ch;
}
}
} String text=null;
try{
text=new String(textBytes,"gb2312");
}catch(UnsupportedEncodingException e){} return text;
} private static int indexOfBase64Table(char ch){
for(int i=0;i<64;i++) if(ch==BASETABLE[i]) return i;
return -1;
}
}
//输入:
BufferedReader in=new BufferedReader(
new InputStreamReader(
socket.getInputStream(),"GBK"));输出:
PrintWrite out=new PrintWrite(
new BufferedWriter(
new OutputStreamWriter(
socket.getOutputStream(),"GBK")),true);
out是你的ps,in是你的dis,socket是一个Socket对象。