乱码问题

有四个方法,记录一下,方便以后自己查找,也方便大家。

首先:
< %@ page language="java" import="java.util.*" pageEncoding="UTF-8" contentType="text/html; charset=UTF-8"%>
并且
request.setCharacterEncoding("gb2312");
第二:
配置 server.xml。

  <Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" URIEncoding="UTF-8" />
  <Connector port="8009" protocol="AJP/1.3" redirectPort="8443"  URIEncoding="UTF-8"/>
第三:
配置 web.xml  
<filter>
  <filter-name>setEncoding</filter-name>
  <filter-class>com.dinner.filter.CharsetFilters</filter-class>
  <init-param>
   <param-name>coding</param-name>
   <param-value>UTF-8</param-value>
  </init-param>
</filter>
<filter-mapping>
  <filter-name>setEncoding</filter-name>
  <url-pattern>/*</url-pattern>
</filter-mapping>
第四:
工程编码
开发中发现Windows版Eclipse默认Java和Web工程的默认编码方式不那么遂人愿,修改方法如下:
1. Web工程文件编码修改方式
Window -> Preference -> Content types
推荐将Web相关文件的编码都设置为UTF-8
2. Java源文件编码修改方式
Window -> Preference -> Workspace -> Text file encoding
Windows平台默认为GBK,Linux平台默认为UTF-8
经过这四个步骤,肯定能解决的。
连接来源:http://blog.sina.com.cn/s/blog_62aab2760100l1hx.html

 

现在项目处理乱码的方法:

后台:this.setName(URLDecoder.decode(this.getAjaxInData(),"utf-8"))

前台:encodeURI($("#diaodumingcheng").val())

server.xml:<Connector port="8009" protocol="AJP/1.3" redirectPort="8443"  URIEncoding="UTF-8"/>

strtuts2图片下载

@Action(value="fileDownload",
   results={@Result(name="success", type="stream", params={
     "contentType", "application/octet-stream;charset=ISO8859-1",
     "inputName", "inputStream",
     "contentDisposition", "attachment;filename=${upPicFileFileName2}",
     "bufferSize", "4096"
     })})
 public String fileDownload() throws FileNotFoundException {
  //upPicFileFileName是服务器存放的文件名
  //upPicFileFileName2是用户上传的名称
  try {
   serv.fileDownload(this);
   String str = new String(this.upPicFileFileName2.getBytes("GBK"),"ISO8859-1");
   this.upPicFileFileName2 = str;
  } catch (UnsupportedEncodingException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  return "success";
 }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值