utf-8项目血泪史
1.inputstrem和string转换时切记带上编码,如:
IOUtils.toString(inputStream, "utf-8");
2.string和byte[]转换时切记带上编码,如:
DigestUtils.md5Hex(sb.toString().getBytes("utf-8")).toUpperCase();
3.url编码解码时候要注意编码,如:
URLEncoder.encode("中文", "utf-8");
4.使用httpclient post传中文时候,对各字段编码,如:
StringPart tempPart = new StringPart(entry.getKey(), (String) entry .getValue(), "utf-8");
5.web.xml里对所有的http输入输出做filter编码, 如:
- <filter>
- <filter-name>encoding</filter-name>
- <filter-class>
- org.springframework.web.filter.CharacterEncodingFilter
- </filter-class>
- <init-param>
- <param-name>forceEncoding</param-name>
- <param-value>true</param-value>
- </init-param>
- <init-param>
- <param-name>encoding</param-name>
- <param-value>UTF-8</param-value>
- </init-param>
- </filter>
- <filter-mapping>
- <filter-name>encoding</filter-name>
- <url-pattern>/*</url-pattern>
- </filter-mapping>