The solution to the problem of java

  • 2020-05-17 05:36:36
  • OfStack

In this paper, the example for you to share the java Chinese transfer value chaotic code problem, and the solution, for your reference, the specific content is as follows

1. General encoding format:

1. It is also the most effective way to decode the character set before insertion after the character set is set twice
Set character set:

String value=null; 
try { value= URLEncoder.encode(jsonObjectPar.getString( " value " ), " UTF-8 " ); } catch (UnsupportedEncodingException e) { e.printStackTrace(); }

Decoded character set:


String value=null; 
try { 
value= new String(value.getBytes( " iso-8859-1 " ), " UTF-8 " ); 
} catch (UnsupportedEncodingException e) { 
e.printStackTrace(); 
} 

2. Direct handling:

userName = new String(userName. getBytes(" ISO-8859-1 "), "UTF-8");

3. If the breakpoint is in Chinese when it is inserted into the database, the database is not set to UTF-8.

First look at the database encoding (MySQL for example):
Run in database query: show variables like 'character%'; View all encoding information
Modify the UTF-8 character set, but check to see if datebase is UTF-8


show variables like  ' character%'; 
+ -- -- -- -- � + -- -- -- -- - -+ 
| Variable_name | Value | 
+ -- -- -- -- � + -- -- -- -- - -+ 
| character_set_client | latin1 | 
| character_set_connection | latin1 | 
| character_set_database | latin1 | 
| character_set_filesystem | binary | 
| character_set_results | latin1 | 
| character_set_server | latin1 | 
| character_set_system | utf8 | 
| character_sets_dir | /usr/share/mysql/charsets/ | 
+ -- -- -- -- � + -- -- -- -- - -+ 

According to the above information, the code of the database is latin1, which needs to be modified to gbk or utf8.
Where, character_set_client is the client encoding method; character_set_connection code used to establish the connection; character_set_database database code; The encoding of character_set_results result set; character_set_server database server encoding, as long as the above four adopted encoding mode 1, there will be no messy code problem.

4. The problem of garbled codes from the background to the page:
Set the character set before returning the data:
response. setCharacterEncoding (" utf - 8 ");

5. Scrambled code from jsp page to background:
5-1. The encoding of jsp is set to utf-8
5-2. Do this backstage


String name = request.getParameter( " ABC " ); 
if(name.equals(new String(request.getParameter( " ABC " ).getBytes( " iso8859-1 " ),  " iso8859-1 " ))) { 
name = new String(name.getBytes( " iso8859-1 " ), " UTF-8 " ); 
} 

The code submitted by get is iso8859-1


Related articles: