客户端这里:
Socket client = new Socket(address,port);
BufferedOutputStream sender = new BufferedOutputStream(client.getOutputStream()); 
String data = 我要发送的中文String;
sender.write(data.getBytes(),0,data.length());
sender.flush();服务器这里:
BufferedReader bufferedReader =httpservletrequest.getReader();
String s = bufferedReader.readLine();s 得到的是问号
网上看到的方法试了都没作用,要怎么转换啊?

解决方案 »

  1.   

    我用这个方法结果很奇怪:
    public static String toChinese(String strvalue){

    try{ 
    if(strvalue==null) return null;
    else{ 
    strvalue = new String(strvalue.getBytes("ISO8859_1"), "GBK"); 
    return strvalue; 

    }catch(Exception e){ 
    return null; 

    } 发出"周"时得到"?"
    发出"周杰"时得到"周"
    发出"周杰伦"时得到"周?"
    发出"周杰伦啊"时得到"周杰"
    发出"周杰伦啊啊"时得到"周杰?"
    发出"周杰伦啊啊啊"时得到"周杰伦"狂汗!!
      

  2.   

    http://www.regexlab.com/zh/encoding.htm
      

  3.   

    sender.write(data.getBytes(),0,data.length());
    这一句出的问题,data.length()返回的是字符串的长度,如“周杰”的length=2
    换成data.getBytes().length
      

  4.   

    楼主可以看BufferedOutputStream 的write方法
    write(byte[] b, int off, int len) 
              Writes len bytes from the specified byte array starting at offset off to this buffered output stream.
    中len是指字节
    楼上正解!!
      

  5.   

    问题应该在这上面,但还是没有解决,看看我测试的结果
    System.out.println(data.length());
    System.out.println(data.getBytes().length);

    sender.write(data.getBytes(),0,data.getBytes().length);
    sender.flush();输入"周杰伦"的时候
    屏幕上
    234
    237
    服务器接到的数据是:"周?"输入"周杰伦aaa"的时候
    屏幕上
    237
    240
    服务器接到的数据是:"周杰伦"
      

  6.   

    换read()方法试试,先读取字节数组,再转换成String试一下
    但是看API,觉得readLine应该没有问题的,楼主试一下吧
      

  7.   

    我这样测试了下:
    发送方:
    String s = new String("findBySinger"+'\n'+"周杰伦");
    System.out.println(s.length());
    System.out.println(s.getBytes().length);
    sender.write(data.getBytes(),0,data.getBytes().length);
    sender.flush();
    然后得到2个数据:
    16
    19
    说明接收方按data.getBytes().length后得到的得到的应该是19接收方:
    int a = httpservletrequest.getContentLength();得到的确是16这说明问题还是在发送方这里
    sender.write(data.getBytes(),0,data.getBytes().length);
    发出去的还是16
    不知道我理解的对不对
      

  8.   

    我找到问题的地方了
    我们改了data.getBytes().length
    但是还有个地方忘记改了
    就是http头部的Content-length
    就是:"Content-length: " + content.getBytes().length
    服务器接到了正文的全部,但是因为Content-length的关系没有读全
    谢谢scrt() !