jsp高手请进,我用jspsmartupload上传文件,但我表单获得的中文是乱码(UTF-8)的,如何解决?

我用了request.setCharacterEncoding("UTF-8");没啥用处,请教

第1个回答  2010-04-25
以下,是中文乱码的各种解决方法,要记住了,很有用,肯定有一种方法能帮你解决的。希望能帮到你。

URIENcoding 设成GBK或GB2312
2. 表单中或传递字符串:本来输入的汉字是正常的,但是提交后再显示出来是乱码,因为提交的一般是 ISO8859编码,所以显示的时候要转成GB2312编码:
String S=new String(rs.getString("news").getBytes("gb2312"),"ISO8859_1");
//rs.getString("news")为待转换的字符串
然后使用S字符串的值就可以了
3. 有的服务器端的语言环境如果设成简体中文的也可以解决此类问题
4. 插入数据库中的字符是乱码
看看数据库中支持的是何种编码方式,用类似2中的方式作一下转换即可。
5. 总之,用jsp开发,碰到乱码,你得分析是读的时候发生乱码,还是写的时候发生乱码,用2中的转换,基本就能解决问题,有些时候写的时候做一次转换,例如:
String S=new String(rs.getString("news").getBytes("gb2312"),"ISO8859_1");
//读的时候在转换回来
String S=new String(rs.getString("news").getBytes("ISO8859_1"),"GB2312");
或者把ISO8859-1和GB2312 的位置换一下,自己多试试,就能找到解决问题的办法。

将乱码问题分为三类JSP页面显示中文乱码;表单提交乱码;数据库应用乱码
1) JSP页面内输出中文时出现乱码
解决方案在JSP文件中使用page命令指定响应结果的MIME类型,如<%@ page language="java" contentType="text/html;charset=gb2312" %>
2)表单提交乱码
表单提交时(post和Get方法),使用request.getParameter方法得到乱码,这是因为tomcat处理提交的参数时默认的是iso-8859-1,表单提交get和post处理乱码问题不同,下面分别说明。
(1)POST处理
对post提交的表单通过编写一个过滤器的方法来解决,过滤器在用户提交的数据被处理之前被调用,可以在这里改变参数的编码方式,过滤器的代码如下:
package cn.gov.beijingit.util;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

public class SetCharacterEncodingFilter implements Filter {

/**
* The default character encoding to set for requests that pass through this
* filter.
*/
protected String encoding = null;

/**
* The filter configuration object we are associated with. If this value is
* null, this filter instance is not currently configured.
*/
protected FilterConfig filterConfig = null;

/**
* Should a character encoding specified by the client be ignored?
*/
protected boolean ignore = true;

// --------------------------------------------------------- Public Methods

/**
* Take this filter out of service.
*/
public void destroy() {

this.encoding = null;
this.filterConfig = null;

}

public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {

// Conditionally select and set the character encoding to be used
if (ignore || (request.getCharacterEncoding() == null)) {
String encoding = selectEncoding(request);
if (encoding != null) {
request.setCharacterEncoding(encoding);
}
}

// Pass control on to the next filter
chain.doFilter(request, response);

}

/**
* Place this filter into service.
*
* @param filterConfig
* The filter configuration object
*/
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;
}

}

protected String selectEncoding(ServletRequest request) {

return (this.encoding);

}

}
web.xml文件加入过滤器
<filter>
<filter-name>Encoding</filter-name>
<filter-class>
cn.gov.beijingit.util.SetCharacterEncodingFilter
</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>gbk</param-value>
<!--gbk或者gb2312或者utf-8-->
</init-param>
<init-param>
<param-name>ignore</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>Encoding</filter-name>
<servlet-name>/*</servlet-name>
</filter-mapping>
* 注意filter元素要放在所有web.xml元素之前。
(2) Get方法的处理
tomcat对post和get的处理方法不一样,所以过滤器不能解决get的乱码问题,它需要在其他地方设置。
打开<tomcat_home>\conf目录下server.xml文件,找到对8080端口进行服务的Connector组件的设置部分,给这个组件添加一个属性:URIEncoding="GBK"。修改后的Connector设置为:
<Connector port="8080" maxHttpHeaderSize="8192"
maxThreads="150" minSpareThreads="25" maxSpareThreads="75"
enableLookups="false" redirectPort="8443" acceptCount="100"
connectionTimeout="20000" disableUploadTimeout="true" URIEncoding="GBK" />
* 注意修改后重新启动tomcat才能起作用。
3)数据库应用的乱码,本文还是以mysql为例
(1)修改配置文件<mysql_home>\my.init
将default-character-set=latin1修改为default-character-set=gbk
(2) 对接受的中文字符重新编码,例
String name=requset.getParameter("name");
name = new String(name.getBytes("gbk"),"iso8859-1");
4)tomcat5.x的include页面乱码
为了能让tomcat5.x能像tomcat4那样处理include页面,需要修改项目web-info/web.xml文件,把默认的字符集设置成gbk/gb2312就可以了,如:
<jsp-config>
<jsp-property-group>
<description>Special property group for JSP Configuration JSP example.</description>
<display-name>JSPConfiguration</display-name>
第2个回答  2010-04-25
以前我也遇到过同样的问题;分析如下,有错的地方可以指出一起分享:
1.request.setCharacterEncoding("UTF-8");必须在获取所有参数前进行设置,也就是要进行所有业务前进行设置。可以选用Filter进行设置,因为过滤器会在处理业务前被激活。
2.有可能是应用服务器的问题以及操作系统问题,可以试一下GBK等多种编码格式.
第3个回答  推荐于2018-03-28
页面最顶部声明页面的编码格式:
请问你服务器是windows还是linux,
如果是linux:<%@ page contentType="text/html;charset=utf-8" language="java" %>试试
如果是windows:<%@ page contentType="text/html;charset=GBK" language="java" %>试试

SmartUpload su = new SmartUpload();
su.initialize(pageContext);
su.upload();//必须放在获得表单参数之前
su.getRequest().getParameter("addsubmit")//获得request参数
com.jspsmart.upload.File files = su.getFiles().getFile(0);
String fileName = new String(files.getFileName());//获得名称

我这边服务在windows平台时只用修改页面编码格式为GBK就一切OK,但是在linux下需要吧页面编码格式改为UTF-8

另外,form表单中的file文件名可能从request得不到,需要通过SmartUpload 类获得文件名,类似上面处理,因为: ENCTYPE="multipart/form-data"本回答被提问者和网友采纳
相似回答