UTF-8 encoding not honored when form has multipart/form-data
Abhishek Asthana
Ranch Hand
Joined: Sep 08, 2004
Posts: 146
posted
0
I have a form in which user can upload a file and another field 'name' in which she can give any name to the file being loaded. (Please see the image in attachements) When I submit the form, the file is uploaded fine but the value in name field is messed up. I have followed all the possible suggestions I found:
1) <%@page pageEncoding="UTF-8"%> set.
2) <%@page contentType="text/html;charset=UTF-8"%> set after the first directive.
3) <meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/> in the head.
4) enctype="multipart/form-data" attribute in the form.
5) accept-charset="UTF-8" attribute in the form.
in the Servlet:
1) before doing any operations on request object: request.setCharacterEncoding("UTF-8");
2) Getting the value:
On the UI, I can see the characters properly but when the value is being passed to java strings, all hell breaks loose!
But nothing seems to be working.
This issue is coming in this form only where enctype is multipart/form-data. Rest everywhere having just 1) and 2) work fine.
Can someone please give some ideas what can be going wrong?
Which servlet container are you using?
Just in case you are using Tomcat, try to set the URIEncoding of your connector in server.xml to UTF-8 (see also: http://wiki.apache.org/tomcat/Tomcat/UTF-8)
Another shot into the dark: have you tried to pass getBytes() your encoding explicitly?
Anyway: good luck!
Abhishek Asthana
Ranch Hand
Joined: Sep 08, 2004
Posts: 146
posted
0
Yes, I tried passing encoding explicitly, but that doesn't work either. I am using Weblogic server. Can we set encoding for weblogic also?
Please Please Please guys, this is very critical for me. Please help!
What's the purpose of the tricky code in line 6? You've told everything in sight to use UTF-8, then you don't trust that, so you start using random encodings instead? Try this instead:
Abhishek Asthana
Ranch Hand
Joined: Sep 08, 2004
Posts: 146
posted
0
Actually I first tried using just
when that didn't work, I used
Abhishek Asthana
Ranch Hand
Joined: Sep 08, 2004
Posts: 146
posted
0
OK, this is the solution:
Instead of
or
do :
carl norburn
Greenhorn
Joined: Oct 24, 2010
Posts: 1
posted
0
I kiss you Abhishek! I was stuck with the multipart/form-data utf8 problem for hours!