////////////////中文转为字符串(进入数据库)
public static String toISO8859(String str) {
if (str == null) {
return null;
}
try {
return new String(str.getBytes("ISO8859_1"));
}
catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return null;
}
public static String toISO8859(String str) {
if (str == null) {
return null;
}
try {
return new String(str.getBytes("ISO8859_1"));
}
catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return null;
}
解决方案 »
- 为什么这种写法,js的setTimeout只执行一次?
- 布局
- 大家来说说程序员的研究方向吧!
- 火了!!既然大家如此现实,我500分求
- 一个小问题:用笔记本编一个空文档存为hello.java,然后用进入cmd shell,编译会出现什么情况?
- 一直没懂,JSP怎么去实现如vB3这种论坛的模板功能
- 求救怎么通过对下拉列表项的选择去控制另一下拉列表的内容
- 请问,javabean怎么在comcat中部署
- 哪有最新jdom下载?
- 求各位 大哥帮看看 为什么 <script type="text/javascript">上传图片他就不编译了
- 字符型的如何转成数值型的?
- 关于ORACLE 9i 执行SELECT top 10 * from tables order by id desc语句出错问题
所以我希望做成一个通用的函数
但是竟然把我的正确的汉字转化成乱码了,晕死
我不知道原理
哪个高手可以告诉我
String str = "error" ;
try{
str=new String(URLDecoder.decode(s).getBytes("ISO-8859-1"), "gb2312"));
return str;
}
catch(Exception e){
return str;
}
}
import java.io.*;
/**
* @author guest1
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*
* setup on web.xml
*<filter>
*<filter-name>Set Character Encoding</filter-name>
*<filter-class>com.learndiary.website.util.SetCharacterEncodingFilter</filter-class>
*<init-param>
*<param-name>encoding</param-name>
*<param-value>utf-8</param-value>
*</init-param>
*<init-param>
*<param-name>ignore</param-name>
*<param-value>true</param-value>
*</init-param>
*</filter>
*/
public abstract class SetCharacterEncodingFilter implements Filter{
protected String encoding = null;
protected FilterConfig filterConfig = null;
protected boolean ignore = true;
public void destory(){
this.encoding = null;
this.filterConfig = null;
}
public void doFilter(ServletRequest request,ServletResponse response,FilterChain chain) throws IOException,ServletException{
if(ignore || request.getCharacterEncoding() == null){
String encoding = selectEncoding(request);
}
if(encoding != null){
request.setCharacterEncoding(encoding);
}
chain.doFilter(request,response);
}
public void init(FilterConfig filterConfig) throws ServletException{
this.filterConfig = filterConfig;
this.encoding = filterConfig.getInitParameter("encoding");
String value = filterConfig.getInitParameter("ignore");
if(value == null){
this.ignore = true;
}else if(value.equalsIgnoreCase("true")){
this.ignore = true;
}else if(value.equalsIgnoreCase("yes")){
this.ignore = true;
}else{
this.ignore = false;
}
}
public String selectEncoding(ServletRequest request){
return this.encoding;
}
}import下面的注释是在web.xml中的配置方法。
<%request.setCharacterEncoding("GB2312");%>
别的不要了