Home  >  Q&A  >  body text

Java 两系统接口调用字符乱码问题?

PHP中文网PHP中文网2741 days ago421

reply all(4)I'll reply

  • 伊谢尔伦

    伊谢尔伦2017-04-18 10:05:23

    You have to understand the meaning of new String(str.getBytes("GBK"),"UTF-8"); to know why it fails.
    First of all, String in Java is internally encoded in UTF-16LE.
    str.getBytes("GBK"), obtained a Byte array, the content is to convert the encoding of str to GBK.
    new String(str.getBytes("GBK"),"UTF-8") treats the contents of this Byte array as UTF-8 encoded data and converts it to String (UTF-16LE).
    Understood? If this works, then there will be ghosts.

    The client and server are both Java, so the String encoding in Java must be UTF-16. Unaffected by system encoding (Windows or Linux).
    In this case, it is best not to send String directly, because String will be automatically converted to encoding by the library you use (take tomcat as an example, the default iso8859-1).

    If you can send and receive Byte arrays directly, then you can definitely specify the protocol as UTF-8. The data received by either party is new String(data, "UTF-8"), and the data sent by either party is str.getBytes("UTF-8").

    If you cannot change the client code or it is written in C or other languages. If you are sure that the client is sending GBK data. If your server can directly receive Byte array, new String(data, "GBK") can be parsed. When returned, it is also the Byte array str.getBytes("GBK").

    If you can only send and receive String, then you need to configure it and set the encoding it converts. For example, for tomcat, for GET data, web.xml needs to be modified and a URL encoding setting item needs to be added. For POST data, you must write a filter to convert . If it is an HTTP communication class, it may provide methods such as SetEncoding.

    reply
    0
  • PHP中文网

    PHP中文网2017-04-18 10:05:23

    If it is a URL, you can use the URLEncoder/URLDecoder class

    reply
    0
  • ringa_lee

    ringa_lee2017-04-18 10:05:23

    If you are the client:
    The string you get from the server is utf-8 encoded. If you need to display it, first decode the string into bytecode according to utf-8, and then encode it according to GBK.
    The string you send to the server must also be utf-8 encoded. You need to decode your GBK-encoded string into bytecode and then encode it according to utf-8.
    Vice versa.
    The reason why you see garbled characters may be the encoding problem of your terminal?

    String gbkStr = new String(utf8Str.getBytes("utf-8"),"gbk");  
    String utf8Str = new String(gbkStr.getBytes("gbk"),"utf-8");  

    reply
    0
  • 迷茫

    迷茫2017-04-18 10:05:23

    It can be encoded as an intermediate string through iso8859-1. If gbk is directly converted to utf–8, that is, 2 bytes are converted to 3 bytes. If there are an odd number of Chinese characters, will an error output be calculated when converting the last byte? , it will still be garbled.

    reply
    0
  • Cancelreply