终结中文乱码

1.字符集介绍

(1)ASCLL码:最早的字符集,美国信息交换标准码。它使用一个字节中的7位来表示一个字符,最多可以表示128个字符;

(2)LATIN1:英语国家和西欧各国的编码标准;

(3)gb2312:我国针对汉字最早提出的一套编码标准,总共可以表示6556个字符;

(4)GBK:gb2312的扩充;

(5)UNICODE:统一了全球的字符编码,支持超过650中语言的国际字符集;

(6)UTF-8:是UNICODE的其中一种使用方式。UTF就是Unicode Translation Format,即把Unicode转换为某种格式。

2.产生乱码的原因

(1)操作系统环境:windows已经能够很好地支持GBK了,如果是linux或Unix则要检查其字符集环境的设置;

(2)数据库环境:库的字符集、表的字符集、字段的字符集;

(3)编程环境:在java中使用response.setContentType()方法可以进行设置;

(4)中间件环境(Tomcat、Weblogic、JBoss):tomcat默认编码是iso-8859-1,可以在server.xml中进行修改;

(5)JSP页面编译时:在头部设置contentType属性值来设置所用的字符集。

3.解决办法

(1)在tomcat中修改server.xml;

<Connector port="8080" maxThreads="150" minSpare="25" maxSpareThreads="75" enableLookups="false" redirectPort="8443" acceptCount="100" connectionTimeout="20000" disableUploadTimeout="true" URIEncoding='GBK' />

(2)修改java编程环境的字符集;

response.setContentType("text/html;charset=GBK");

(3)mysql数据库所采用的字符集;

创建数据库时加上default charset=GBK

(4)JSP页面编辑时。

连接数据库时:

con=DriverManager.getconnection("jdbc:mysql://localhost:3306/sample_db?user=&password=&useUnicode=true&characterEncoding=GBK");

4.小结

(1)如果怕出现乱码,尽量在JSP页面中加上如下代码:

<%@page contentType="text/html;charaset=GBK"%>
<META http-equiv=Content-Type content="text/html;charset=GBK">

(2)最为糟糕的是进行了以上的设置还不能解决乱码问题,就需要为Tomcat服务器添加一个设置字符集的Filter类。

package filters;
import java.io.IOException;
import javax.servlet.*;

public class SetCharacterEncodingFilter implements Filter {
  protected String encoding = null;
  protected FilterConfig filterConfig = null;
  protected boolean ignore = true;
  public void destroy() {
   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.ignure = true;
   else
     this.ignore = false;
  }
  protected String selectEncoding(ServletRequest request) {
  return (this.encoding);
  }
}
然后再web.xml中进行相应的配置:

<filter>
<filter-name>Set Character Encoding</filter-class>
<filter-class>filters.SetCharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>GBK</param-value>
</init-param>
</filter>

<filter-mapping>
<filter-name>Set Character Encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Graphics2D中出现中文乱码的问题通常是由于字体设置不正确导致的。为了解决这个问题,你可以按照以下步骤进行操作: 1. 确保你的操作系统中已经安装了支持中文的字体,比如宋体、微软雅黑等。 2. 在绘制文本之前,使用setFont()方法设置字体。例如,你可以使用以下代码设置字体为宋体: ``` Font font = new Font("宋体", Font.PLAIN, 12); graphics2D.setFont(font); ``` 3. 确保你的文本编码方式与字体设置一致。如果你的文本是以UTF-8编码保存的,那么可以使用以下代码设置编码方式: ``` graphics2D.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON); graphics2D.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_GASP); graphics2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); ``` 4. 如果你绘制的文本中包含了特殊字符或者使用了自定义字体,你还可以尝试使用Font.createFont()方法加载字体文件,并设置为当前的字体。例如,你可以使用以下代码加载自定义字体文件: ``` Font font = Font.createFont(Font.TRUETYPE_FONT, new File("font.ttf")); graphics2D.setFont(font.deriveFont(12.0f)); ``` 通过以上步骤,你应该能够解决在Graphics2D中出现中文乱码的问题。如果问题仍然存在,你可以提供更多的代码和错误信息,以便更好地帮助你解决问题。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值