In Java the solution of URL's scrambled Chinese characters

  • 2020-05-10 18:07:02
  • OfStack

preface

In Java, I believe many of my friends have encountered the problem of confusing Chinese characters when URL transmits Chinese characters. Recently, I encountered a problem, that is, to bind a Chinese message to the back of URL in Action, and to use ActionForward for another page reqeust.getParameter Take out is the problem that appears garbled code.

The solution

1. Encode the Chinese characters to be transferred by URL:


String message = java.net.URLEncoder.encode(" Chinese characters ","utf-8");

2. Decode the characters on the page where URL is transferred to Chinese:


String msg = request.getParameter("message");
String str=new String(msg.getBytes("ISO-8859-1"),"UTF-8");

Note:

        1. Here, str is the "Chinese character" passed in earlier.

        2. The reason for converting the extracted character set into UTF-8 is that ISO-8859-1 is the standard character set for network transmission in Java. request.getParameter(“message”); You still get the ISO-8859-1 character set, so you have to convert 1.

conclusion

The above is the whole content of this article, I hope the content of this article to your study or work can bring 1 definite help, if you have questions you can leave a message to communicate.


Related articles: