對這文章發表回應
發表限制: 非會員 可以發表
發表者: 冷日 發表時間: 2011/1/18 15:59:28
jsp中使用multipart/form-data類型的form提交亂碼問題
項目時發現以前是application/x-www-form-urlencoded的FORM提交數據時正常, 現在為表單添加個上傳功能就有亂碼了, 在網上看了篇文章
引言:
看起來在Jetty上跑沒事, 好像是Tomcat的編碼問題, 解決方法也簡單, page charset 用utf8就好了, 如果你用spring就加個filter:
原文出處:
jsp中使用multipart/form-data类型的form提交乱码问题 - Wonderful Dream™ - JavaEye技术网站
冷日補充:
沒想到冷日也碰上類似的問題了,確認了資料庫、前端網頁、Form的編碼以後,已經很確認冷日都是使用『UTF-8』了!(謎:冷日本來就愛用UTF-8.....
也就把目標轉到Form的encoding上(可以參閱這篇:[轉貼]javascript構造可以上傳文件的form表單)!
也曾經在網路上看到:「enctype="multipart/form-data"是不能传递表单数据的」,經實驗,這句話是唬爛低!
結論是,Tomcat碰到『multipart/form-data』時,是使用「ISO-8859-1」這個編碼!
只要我們收資料時,做一個轉碼,哪有啥multipart/form-data不能傳遞表單數據低道理?給大家一個範例:
這樣就可以收到正常編碼的資料啦,祝大家順利!
項目時發現以前是application/x-www-form-urlencoded的FORM提交數據時正常, 現在為表單添加個上傳功能就有亂碼了, 在網上看了篇文章
引言:
This problem had been bugging me for 3 days!
Background:
Use JSP and Servlet to implement file upload。The uploaded file is stored in file system, the related info like description and Mime type, size and path name is stored in MySQL。JSP and DB table both use UTF-8 CHARSET. If successfully,Servelet forwards to success page with related file info returned.
Promblem(all happened with Chinese characters):
1. On Jetty,except the filename was encoded by UTF-8(unreadable), everything is good.
2. But on Tomcat, all that characters were inserted into DB and returned to client were corrupted.
Solution:
1.No matter how you set a form's charset,Tomcat always treats it by iso 8859-1.So what we read from the input strem are 8859-1 encoded.
If we need to search a substring in the content, we have to use getBytes("ISO-8859-1") on the substring. Also, use String(subBytes, "UTF-8") to return some substrings.
2.In the success jsp, we should add <%@ page contentType="text/html;charset=UTF-8"%>.
(And the is useless.)
3.In the post() method of Servlet, we must add request.setCharacterEncoding("UTF-8"); And it must sit before any sentences that read input stream.
After the previous steps, everything is good on Tomcat.
But on Jetty, the filename was still encoded by UTF-8. I tried to convert UTF-8 to system default encoding by new String(fileName.getBytes("UTF-8"), system.getProperty("file.encoding")), but no use.
看起來在Jetty上跑沒事, 好像是Tomcat的編碼問題, 解決方法也簡單, page charset 用utf8就好了, 如果你用spring就加個filter:
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
原文出處:
jsp中使用multipart/form-data类型的form提交乱码问题 - Wonderful Dream™ - JavaEye技术网站
冷日補充:
沒想到冷日也碰上類似的問題了,確認了資料庫、前端網頁、Form的編碼以後,已經很確認冷日都是使用『UTF-8』了!(謎:冷日本來就愛用UTF-8.....
也就把目標轉到Form的encoding上(可以參閱這篇:[轉貼]javascript構造可以上傳文件的form表單)!
也曾經在網路上看到:「enctype="multipart/form-data"是不能传递表单数据的」,經實驗,這句話是唬爛低!
結論是,Tomcat碰到『multipart/form-data』時,是使用「ISO-8859-1」這個編碼!
只要我們收資料時,做一個轉碼,哪有啥multipart/form-data不能傳遞表單數據低道理?給大家一個範例:
dataSource2Insert = new String(value.getBytes("ISO-8859-1"), "UTF-8") ;
這樣就可以收到正常編碼的資料啦,祝大家順利!