您的位置:首页 > 其它

PrimeFaces <p:fileUpload>上传文件 中文乱码

2013-07-17 11:02 627 查看
某度搜索出来的,关于PrimeFaces <p:fileUpload>上传文件中文乱码的问题,基本没解决得了,所以Google了一下,找到了解决办法;并整理了一下,内容如下( 此方法的好处就是不用编辑源文件):

一、PrimeFaces version:

primefaces-3.2.jar

二、相关文档:
http://code.google.com/p/primefaces/issues/detail?id=787
The following is Simplified Chinese.

当使用PrimeFaces的<p:fileUpload>上传文件并使用非拉丁字符(比如中文)时,服务器端接收的文件名会编码混乱(即中文乱码),表单中其它输入的文本也会编码混乱(即中文乱码)。
这是一个编码问题。

相关论坛主题连接:
http://primefaces.prime.com.tr/forum/viewtopic.php?f=3&t=1963
http://primefaces.prime.com.tr/forum/viewtopic.php?f=3&t=6513

另请参阅《解决Commons FileUpload的中文乱码问题》:http://blog.csdn.net/luhao/archive/2007/07/16/1693641.aspx

我在论坛里登记了,但我无法接收到激活邮件。
因此,我无法登录进论坛,也无法在论坛里添加评论 :-(

我找到修复这个问题的解决办法了,仅用3步:

步骤1. 修复 primefaces-read-only\src\main\java\org\primefaces\webapp\filter\FileUploadFilter.java

Index: FileUploadFilter.java
===================================================================
--- FileUploadFilter.java	(版本 4615)
+++ FileUploadFilter.java	(工作副本)
@@ -69,6 +69,10 @@
}

ServletFileUpload servletFileUpload = new ServletFileUpload(diskFileItemFactory);
+                        String encoding = request.getCharacterEncoding();
+                        if (encoding != null) {
+                            servletFileUpload.setHeaderEncoding(encoding);
+                        }
MultipartRequest multipartRequest = new MultipartRequest(httpServletRequest, servletFileUpload);

if(logger.isLoggable(Level.FINE))

步骤2. 修复 primefaces-read-only\src\main\java\org\primefaces\webapp\MultipartRequest.java

Index: MultipartRequest.java
===================================================================
--- MultipartRequest.java	(版本 4615)
+++ MultipartRequest.java	(工作副本)
@@ -16,6 +16,7 @@
package org.primefaces.webapp;

import java.io.IOException;
+import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;
@@ -24,6 +25,7 @@
import java.util.List;
import java.util.Map;
import java.util.Set;
+import java.util.logging.Level;
import java.util.logging.Logger;

import javax.servlet.http.HttpServletRequest;
@@ -81,11 +83,23 @@
}

private void addFormParam(FileItem item) {
-		if(formParams.containsKey(item.getFieldName())) {
-			formParams.get(item.getFieldName()).add(item.getString());
+		String str = null;
+		String charset = this.getRequest().getCharacterEncoding();
+		if (charset == null) {
+			str = item.getString();
} else {
+			try {
+				str = item.getString(charset);
+			} catch (UnsupportedEncodingException ex) {
+				str = item.getString();
+				Logger.getLogger(MultipartRequest.class.getName()).log(Level.SEVERE, null, ex);
+			}
+		}
+		if (formParams.containsKey(item.getFieldName())) {
+			formParams.get(item.getFieldName()).add(str);
+		} else {
List<String> items = new ArrayList<String>();
-			items.add(item.getString());
+			items.add(str);
formParams.put(item.getFieldName(), items);
}
}

步骤3. 修复 primefaces-examples\showcase\src\main\webapp\WEB-INF\web.xml (CharacterEncodingFilter应该放在FileUploadFilter的前面)

Index: web.xml
===================================================================
--- web.xml	(版本 4615)
+++ web.xml	(工作副本)
@@ -37,20 +37,20 @@
-->

<filter>
-        <filter-name>PrimeFaces FileUpload Filter</filter-name>
-        <filter-class>org.primefaces.webapp.filter.FileUploadFilter</filter-class>
+        <filter-name>Character Encoding Filter</filter-name>
+        <filter-class>org.primefaces.examples.filter.CharacterEncodingFilter</filter-class>
</filter>
<filter-mapping>
-        <filter-name>PrimeFaces FileUpload Filter</filter-name>
+        <filter-name>Character Encoding Filter</filter-name>
<servlet-name>Faces Servlet</servlet-name>
</filter-mapping>

<filter>
-        <filter-name>Character Encoding Filter</filter-name>
-        <filter-class>org.primefaces.examples.filter.CharacterEncodingFilter</filter-class>
+        <filter-name>PrimeFaces FileUpload Filter</filter-name>
+        <filter-class>org.primefaces.webapp.filter.FileUploadFilter</filter-class>
</filter>
<filter-mapping>
-        <filter-name>Character Encoding Filter</filter-name>
+        <filter-name>PrimeFaces FileUpload Filter</filter-name>
<servlet-name>Faces Servlet</servlet-name>
</filter-mapping>


三、根据上面的内容,在项目中新建同名文件:



四、在web.xml加入如下内容:

<filter>
<filter-name>Character Encoding Filter</filter-name>
<filter-class>cn.ems.budget.global.CharacterEncodingFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>Character Encoding Filter</filter-name>
<servlet-name>Faces Servlet</servlet-name>
</filter-mapping>

<filter>
<filter-name>PrimeFaces FileUpload Filter</filter-name>
<filter-class>org.primefaces.webapp.filter.FileUploadFilter</filter-class>
<init-param>
<param-name>thresholdSize</param-name>
<param-value>10000000</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>PrimeFaces FileUpload Filter</filter-name>
<servlet-name>Faces Servlet</servlet-name>
</filter-mapping>


五、CharacterEncodingFilter文件:

package cn.ems.budget.global;

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 CharacterEncodingFilter implements Filter{
public void destroy() {

}

public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
request.setCharacterEncoding("UTF-8");
System.out.println("getCharacterEncoding:"+request.getCharacterEncoding());
response.setCharacterEncoding("UTF-8");
chain.doFilter(request, response);

}

public void init(FilterConfig filterConfig) throws ServletException {

}
}


六、效果:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: