日期:2014-05-19  浏览次数:20804 次

纯java代码中如何配置字符集
求各位高手指点在纯java的代码中如何配置字符集,之前一道面试题。我答的是request.setCharacterEncoding("UTF-8");和response.setContentType("text/html;charset=UTF-8");可是对面告诉我不对,我在网上也没找到答案,求各位高手指点一下。

------解决方案--------------------
String fileEncoding = System.getProperty("file.encoding"); 

设置属性有以下三种:
1.你就是没有保存回去,就差一步了
Properties pps=System. getProperties(); 
pps.put("file.encoding","x-MacThai");
//设置完成后要将属性保存
System.setProperties(pps);
2.System.setProperty("file.encoding","x-MacThai"); 
3.运行时设置 java -D file.encoding=x-MacThai

------解决方案--------------------
request和response这个只是在java web里才有。java里改变字符集应该是:
System.setProperty("file.encoding","字符集类型(utf-8等)");
然后可以查看字符集类型System.getProperty("file.encoding");