aspose file tools*
The moose likes JSP and the fly likes UTF-8 encoding not honored when form has multipart/form-data Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "UTF-8 encoding not honored when form has multipart/form-data" Watch "UTF-8 encoding not honored when form has multipart/form-data" New topic
Author

UTF-8 encoding not honored when form has multipart/form-data

Abhishek Asthana
Ranch Hand

Joined: Sep 08, 2004
Posts: 146
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?

Thanks
Abhishek



[Thumbnail for img.PNG]

Joachim Rohde
Ranch Hand

Joined: Nov 27, 2006
Posts: 423

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
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!

Thanks
Abhishek
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18987
    
    8

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
Actually I first tried using just

when that didn't work, I used


Abhishek Asthana
Ranch Hand

Joined: Sep 08, 2004
Posts: 146
OK, this is the solution:
Instead of

or


do :
carl norburn
Greenhorn

Joined: Oct 24, 2010
Posts: 1
I kiss you Abhishek! I was stuck with the multipart/form-data utf8 problem for hours!

item.getString("UTF-8").trim()

works a treat

Thanks
Sendhil Kumar S
Greenhorn

Joined: Sep 15, 2011
Posts: 5

@carl norburn Am still getting the same error.? May i have the full code please.! Am stuck with this for days..??
Sendhil Kumar S
Greenhorn

Joined: Sep 15, 2011
Posts: 5

Ya..got the output...Thanks for the support..
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: UTF-8 encoding not honored when form has multipart/form-data