Servlet response of the garbage problem

A .response has two output stream (encoded: UTF-8):

1.字节流:response.getOutputStream().write(date.getBytes("UTF-8"));

// let the browser with utf8 to parse the data returned 
response.setHeader ( "Content-of the type", "text / HTML; charset = UTF-8" ); 
String the Data = "test garbled" ; 
. Response.getOutputStream () the Write ( data.getBytes ( "UTF-. 8"));

 

2. character stream: response.getWrite () write (); to set response.setCharacterEncoding ( "UTF-8");.

// 1 told servlet server with UTF-8 transcoding, rather than using the default ISO8859-1 
response.setCharacterEncoding ( "UTF-8");
 // 2 allows browser with utf8 to parse the returned data 
response.setHeader ( "the Content-type", "text / HTML; charset = UTF-. 8" ); 
String Data = "distortion test" ; 
. response.getWrite () Write (Data);

PS:

1. If the Chinese character returns appear ????, which showed no increase response.setCharacterEncoding ( "UTF-8"); this sentence.

2. If the returned Chinese is "Quan marsh" This garbled instructions to resolve the problem browser, you forget to add response.setHeader under should check ( "Content-type", "text / html; charset = UTF-8") ;This sentence.

 

Quote: https://blog.csdn.net/TLMS_/article/details/78749980

Guess you like

Origin www.cnblogs.com/ayeex/p/11706487.html