Problems uploading a file in UTF-8 using Tomcat / Struts

R

Robert Bowen

I am using Struts' FormFile class to upload a text file from a
multipart form.

Everything works fine in Windoze but now that we have installed it in
Linux we are getting an error reading the text file, it says it can't
read it because it is in UTF-8.

We are working in Spanish and as such have characters with accents,
etc. Do I have to tell Tomcat (in the web.xml with the
SetCharacterEncodingFilter filter) to treat the file as ISO-8859-1?

I read that this filter is useful when you have a multipart form with
fields of both type File and normal text fields, so that the normal
text fields are encoded correctly, but that it doesn't affect File
fields. And that in order to upload the file correctly I would need
something like the Jakarta Commons upload utility class. But I imagine
the Struts FormFile uses commons anyway, no?

I've never had to do this for any other application but then again,
anything is possible. Any help much appreciated, this problem is
driving me NUTS!

Thanks,
syg
 
D

Dave Glasser

(e-mail address removed) (Robert Bowen) wrote on 12 Jan 2005 09:00:17 -0800 in
comp.lang.java.programmer:
I am using Struts' FormFile class to upload a text file from a
multipart form.

Everything works fine in Windoze but now that we have installed it in
Linux we are getting an error reading the text file, it says it can't
read it because it is in UTF-8.

We are working in Spanish and as such have characters with accents,
etc. Do I have to tell Tomcat (in the web.xml with the
SetCharacterEncodingFilter filter) to treat the file as ISO-8859-1?

I read that this filter is useful when you have a multipart form with
fields of both type File and normal text fields, so that the normal
text fields are encoded correctly, but that it doesn't affect File
fields. And that in order to upload the file correctly I would need
something like the Jakarta Commons upload utility class. But I imagine
the Struts FormFile uses commons anyway, no?

I don't know anything about the Struts FormFile class, but I'm
wondering why it would would care about the encoding of the uploaded
text file. The uploaded file should be treated like a stream of binary
data, because, in reality, it could be any type of file--an image, a
Word document, a zip file, etc. Once you get the data uploaded, then
you can use your own code to figure out its encoding and do any
necessary conversions to it.

Sometimes a user's browser is set to use UTF-8 encoding, and form
parameters will be encoded that way, and that can cause problems if
you don't deal with it (especially when you have high-ASCII characters
with accents, etc.) Even if that's the case you're dealing with, I
still don't know why that should have any effect on the uploaded file
data. Like I said, the object that handles reading it from the stream
and possibly writing it to disk should be agnostic about what type of
data it actually is.


--
Check out QueryForm, a free, open source, Java/Swing-based
front end for relational databases.

http://qform.sourceforge.net

If you're a musician, check out RPitch Relative Pitch
Ear Training Software.

http://rpitch.sourceforge.net
 
S

sygsix

Hi there, thanks for the response.

In the end I figured it out, with a little help from Google Groups. It
turns out our Red Hat Linux is installed in english, UTF-8. My web
pages, the the file we are uploading are both encoded in ISO-8859-1. We
changed the file:

/etc/sysconfig/i18n

so that is uses ISO instead of UTF et voila.

We *should* be able to control it in our Webapp, and in fact we tried
changing all kinds of things, from the web.xml (putting a
javaEncoding=ISO-8859-1 as an init param for the Struts Action
Servlet), to changing our Actions so it changed the encoding scheme of
the request object. And nothing worked.

I read in one of the messages that we shouldn't rely on this, that we
should "defend" from these kinds of problems in the app, and not assume
that the client and server have the same character encoding.
Specifically it was suggested that when you create a FileInputStream,
or BufferedInputStream, or whatever object you use to read an uploaded
file, that you set the character encoding first. All well and good but
since we are using Strut's FileForm, which I think uses the Jakarta
commons FileUpload classes, it wasn't that simple to make that change.
I took a brief look at the FileForm class and didn't see anything about
encoding. And since we've been struggling with this problem the whole
week, and our client is starting to get p'd off ...

Anyway, it's all in the interest of learning, no? And getting paid ...

Thanks again, cheers!
syg
 
Joined
Feb 11, 2009
Messages
1
Reaction score
0
Hi
I'm new to Struts and I realize this post is very old.
I'm working the upload file in Struts... and my file manages to upload perfectly well. I can access the file by double clicking on it. However when I try to read the file through java, it displays the path correctly but gives a file not found exception.
Could you guide me on what could be wrong?

Thanks in advance,
Jo
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,001
Messages
2,570,255
Members
46,853
Latest member
GeorgiaSta

Latest Threads

Top