This problem had been bugging me for 3 days!
Background:
Use JSP and Servlet to implement file upload。The uploaded file is stored in file system, the related info like description and Mime type, size and path name is stored in MySQL。JSP and DB table both use UTF-8 CHARSET. If successfully,Servelet forwards to success page with related file info returned.
Promblem(all happened with Chinese characters):
1. On Jetty,except the filename was encoded by UTF-8(unreadable), everything is good.
2. But on Tomcat, all that characters were inserted into DB and returned to client were corrupted.
Solution:
1.No matter how you set a form's charset,Tomcat always treats it by iso 8859-1.So what we read from the input strem are 8859-1 encoded.
If we need to search a substring in the content, we have to use getBytes("ISO-8859-1") on the substring. Also, use String(subBytes, "UTF-8") to return some substrings.
2.In the success jsp, we should add <%@ page contentType="text/html;charset=UTF-8"%>.
(And the <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> is useless.)
3.In the post() method of Servlet, we must add request.setCharacterEncoding("UTF-8"); And it must sit before any sentences that read input stream.
After the previous steps, everything is good on Tomcat.
But on Jetty, the filename was still encoded by UTF-8. I tried to convert UTF-8 to system default encoding by new String(fileName.getBytes("UTF-8"), system.getProperty("file.encoding")), but no use.