解决方法就是这段代码,其中item是fileupload中的FileItem
String value = new String(item.getString().getBytes("ISO-8859-1"), "UTF-8") ;
但是说老实话,这个解决方法不具有通用性,换个环境很可能就不能用了,比如刚才,我用到smartupload上,就给我来了个exception
1、获取form中字段(即除了上传的文件以外的那些元素)是中文,如果出现了乱码,使用了如下代码,解决了乱码的问题
String value = item.getString("UTF-8");
2、如果上传的文件名是中文,如果出现了乱码,只要设置了request.setCharacterEncoding("UTF-8")就可以解决
完整代码如下:
submit.html
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <form action=".jspfileupload_demo01.jsp" method="post" enctype="multipart/form-data"> 姓名:<input type="text" name="uname"><br> 照片:<input type="file" name="pic"><br> <input type="submit" value="上传"> <input type="reset" value="重置"> </form> </body> </html>
display.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ page import="org.apache.commons.fileupload.*" %> <%@ page import="org.apache.commons.fileupload.disk.DiskFileItemFactory" %> <%@ page import="org.apache.commons.fileupload.servlet.*" %> <%@ page import="java.util.List" %> <%@ page import="java.util.Iterator;" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <% request.setCharacterEncoding("UTF-8") ; %> <% DiskFileItemFactory factory = new DiskFileItemFactory() ; ServletFileUpload upload = new ServletFileUpload(factory) ; upload.setFileSizeMax(3 * 1024 * 1024) ; // 单个文件大小3M封顶 List<FileItem> items = upload.parseRequest(request) ; // 接收全部内容 Iterator<FileItem> iter = items.iterator() ; while(iter.hasNext()){ FileItem item = iter.next() ; String fieldName = item.getFieldName() ; // 取得表单控件的名称 %> <ul><h4><%=fieldName%> --> <%=item.isFormField()%></h4> <% if(!item.isFormField()){ // 不是普通文本 String fileName = item.getName() ; // 取得文件的名称 String contentType = item.getContentType() ; // 文件类型 long sizeInBytes = item.getSize() ; %> <li>上传文件名称:<%=fileName%> <li>上传文件类型:<%=contentType%> <li>上传文件大小:<%=sizeInBytes%> bytes <% } else { String value = item.getString("UTF-8"); %> <li>普通参数:<%=value%> <% } %> </ul> <% } %> </body> <h2>上传成功,<a href="..fileupload_demo01.html">返回</a></h2> </html>
下面是在网上找到的资料:
-------------------------------------
最近一直在整上传图片的东西,改了很长时间,实验了3,4种方法,最终还是选择了commons fileupload这个上传组件~~
最后阶段出现的乱码很严重,不仅有文件名称的中文乱码,还有非文件的表单字段的乱码,也查了好多前辈的资料,最后的已解决。改动如下,和大家共享:
首先说明下我用的组建方法
DiskFileItemFactory dfif = new DiskFileItemFactory();
ServletFileUpload sfu = new ServletFileUpload(dfif);
再者我没有忽略简单form字段,项目中用到了~~
1:写一个过滤器吧,对所有的request请求都使用utf-8编码,有了它,所有乱码都不怕不怕啦
// 这个方法我试了一下,在%WEB-ROOT%/WEB-INF/web.xml中添加了,但是没有什么明显效果,可能是我哪里没用对,我配置的具体情况见listting 1
package up; 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; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class CharacterEncodingFilter implements Filter { private String config; public void destroy() { this.config = null; } public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { HttpServletRequest req; req = (HttpServletRequest) request; HttpServletResponse res; res = (HttpServletResponse) response; req.setCharacterEncoding(config); res.setCharacterEncoding(config); chain.doFilter(req, res); } public void init(FilterConfig config) throws ServletException { this.config = config.getInitParameter("chars"); } }
下面的配置也非常的重要,不可忽略!!在web.xml里面加上下面的一段话:
<filter> <filter-name>CharacterEncodingFilter</filter-name> <filter-class>up.CharacterEncodingFilter</filter-class> <init-param> <param-name>chars</param-name> <param-value>utf-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>CharacterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
2.在上传的servlet中取得request的编码格式:
// 我不太明白这个所谓的“上传的servlet”指的是谁?反正我在接收form的页面使用request.getCharacterEncoding()得到的是null
String encoding=request.getCharacterEncoding();
3.解决上传文件的路径乱码问题:
ServletFileUpload sfu = new ServletFileUpload(dfif);之后加上:
sfu.setHeaderEncoding(encoding);
4:解决表单字段的乱码:
String(fileItem.getString(encoding));
比如其中一项表单:
String id1=new String(fileItem.getString(encoding));
以此类推。
5:这时候,如果还有问题,就可能是tomcat的配置问题了,找到server.xml,寻找到如下字段模仿修改即可
<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" URIEncoding="UTF-8"/>
这几步完成后,乱码问题就彻底摆脱掉了~~~
listing 1.
下面是这个filter,我编译好之后放到了%WEB-ROOT%/WEB-INF/classes中
package cn.mldn.lxh.encoding.filter; 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; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class CharacterEncodingFilter implements Filter { private String config; public void destroy() { this.config = null; } public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { HttpServletRequest req; req = (HttpServletRequest) request; HttpServletResponse res; res = (HttpServletResponse) response; req.setCharacterEncoding(config); res.setCharacterEncoding(config); chain.doFilter(req, res); } public void init(FilterConfig config) throws ServletException { this.config = config.getInitParameter("chars"); } }
这个是%WEB-ROOT%/WEB-INF/web.xml
<?xml version="1.0" encoding="ISO-8859-1"?> <web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5"> <display-name>Welcome to Tomcat</display-name> <description>Welcome to Tomcat</description> <servlet> <servlet-name>he</servlet-name> <jsp-file>/WEB-INF/hello.jsp</jsp-file> </servlet> <servlet-mapping> <servlet-name>he</servlet-name> <url-pattern>/hello.mldn</url-pattern> </servlet-mapping> <servlet> <servlet-name>dbinit</servlet-name> <jsp-file>/WEB-INF/init.jsp</jsp-file> <init-param> <param-name>driver</param-name> <param-value>org.gjt.mm.mysql.Driver</param-value> </init-param> <init-param> <param-name>url</param-name> <param-value>jdbc:mysql://localhost:3306/mldn</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>dbinit</servlet-name> <url-pattern>/config.mldn</url-pattern> </servlet-mapping> <!-- 这个就是我配置的那个filter --> <filter> <filter-name>CharacterEncodingFilter</filter-name> <filter-class>cn.mldn.lxh.encoding.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>chars</param-name> <param-value>utf-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>CharacterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>