SWFUpload is a file upload tool developed using Flash and Javascript. Recently, when using it to upload files, it found that Chinese characters returned from the background are often garbled.
After SWFUpload is uploaded successfully, the following operations are performed on the returned results in the background:
function uploadSuccess(file, serverData) { ......}
Here, file is the information of the uploaded file, and serverData is the data returned from the background. We found garbled Chinese Characters in serverData. The Code returned by the Struts2 Action class for processing image uploads in the background is as follows:
servletResponse.getOutputStream().println(serverData);
Later I found that something went wrong in the above area. Previously, the println method of the ServletOutputStream byte stream was used. This method will convert the serverData string to the byte stream according to the default encoding, while SWFUpload uses UTF-8 encoding, resulting in inconsistent encoding, chinese garbled characters.
Then I thought about two solutions:
1) For byte streams, manually encode serverData according to the UTF-8, and use the write method to output byte streams;
servletResponse.getOutputStream().write(serverData.getBytes("UTF-8"));
2) use the livestream and specify the encoding of the livestream as the UTF-8.
servletResponse.setCharacterEncoding("UTF-8");servletResponse.getWriter().print(serverData);
This article is from the "Learning document" blog, please be sure to keep this source http://zephiruswt.blog.51cto.com/5193151/1294189