aspose file tools*
The moose likes Servlets and the fly likes Uploading file with special characters in name Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Uploading file with special characters in name" Watch "Uploading file with special characters in name" New topic
Author

Uploading file with special characters in name

Kovacs Antal
Greenhorn

Joined: May 17, 2010
Posts: 11
Hi!

I am using the Apache Commons Fileupload tools for standard file upload. My problem is that I cannot get the proper filename of uploaded files if they contain special characters (á, é, ú, etc.) They all get converted to ? signs.

request.getCharacterEncoding() says UTF-8, but the bytes I get in the string fileItem.getName() are all the same for all my special characters.

Can you help me what's wrong?

(Some details: using Firefox 3.6.12, Weblogic 10.3 on Windows)

This is my code snippet:

Shankar Tanikella
Ranch Hand

Joined: Jan 30, 2011
Posts: 329

Not sure about this at all, never used it though,
but happened to see the API just now and found that there is "setHeaderEncoding" for ServletFileUpload and default value is not UTF-8 and also happened to see that it gets the file name from the header.
May be it may work, just give a try and set the header encoding to UTF-8 or whatever desired


Have Fun with Java
little,little.. little by little makes a lot..
Kovacs Antal
Greenhorn

Joined: May 17, 2010
Posts: 11
Well, as I understand the browser tells the encoding of its message. I think when the request is parsed this should be considered when I ask for the fileItem's name at fileItem.getName().. The Apache javadoc also says for setHeaderEncoding: "When not specified, or null, the request encoding is used."

In my case the enc = request.getCharacterEncoding(); resulted in "UTF-8", so I think this is what the browser sends. But why the parser is unable to get the correct filename then.. ? :-(
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

It isn't clear to me why you think there is a problem. You have a lot of experimental code, it appears, but nothing which tells me what you looked at to determine that you thought there was a problem. Could you provide more information about how you looked at the file name in that code?
Kovacs Antal
Greenhorn

Joined: May 17, 2010
Posts: 11
Debugging / logging shows me that fileItem.getName() gives me a String that contains ? marks at all places where special characters (á, é, ö, stb.) should be. Uploaded files can be downloaded later in my program, filenames are bad when downloaded too..
Kovacs Antal
Greenhorn

Joined: May 17, 2010
Posts: 11
thank you all, the problem was solved by calling setHeaderEncoding("iso-8859-2") explicitly, as you suggested.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Uploading file with special characters in name