例如若瀏覽器使用UTF-8處理字元,Web容器預設使用ISO-8859-1編碼,則正確處理編碼的方式為:
<%
String val = request.getParameter("data");
String encode = new String(val.getBytes("ISO-8859-1"), "UTF-8");
out.print("Data: " + encode);
%>
JAVA API String
constructor
String(byte[] bytes, Charset charset)
Constructs a new
String
by decoding the specified array of bytes using the specified charset.
method
getBytes
public byte[] getBytes(String charsetName) throws UnsupportedEncodingException
Encodes this
String
into a sequence of bytes using the named charset, storing the result into a new byte array.
沒有留言:
張貼留言