commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Thomas Zastrow <>
Subject Unicode Upload with FileUpload
Date Mon, 13 Jul 2009 12:51:01 GMT

I'm new to this list and hopefully it is OK to ask my question here ... ;-)

I'm using FileUpload to get data from a HTML form via POST method 
(Apache Tomcat). It works fine, but the encoding of the file is always 
damaged. For example, I have text files (XML) in UTF-8 encoding with 
german special characters, but when uploading the file and printing its 
content to the browser, all special characters are gone or replaced by 
something strange.

This is the code I use (avoiding to write the file to the harddisk, I 
used the most simple way from the manual):

            // Create a factory for disk-based file items
            FileItemFactory factory = new DiskFileItemFactory();

            // Create a new file upload handler
            ServletFileUpload upload = new ServletFileUpload(factory);

            // Parse the request
            List /* FileItem */ items = upload.parseRequest(request);

            String xml = "";

            Iterator iter = items.iterator();
            while (iter.hasNext()) {
                FileItem fi = (FileItem);
                if (fi.isFormField() && 
fi.getFieldName().equals("nameCorpus")) {
                    nameCorpus = fi.getString();
                } else {
                    byte[] data = fi.get();
                    int c;
                    for (i = 0; i < data.length; i++) {
                        c = data[i];
                        xml = xml + (char) c;
                    //out.print(  (char)c );

                }// if else
            } //while



It would be nice, when somebody has an idea how I can preserve the 
Unicode content of the file ...?

Thank you very much in advance,


Thomas Zastrow
Seminar fuer Sprachwissenschaft
Universitaet Tuebingen

Wilhelm Str. 19
D-72074 Tuebingen

Tel.: 07071/29-73968
Fax: 07071/29-5214

To unsubscribe, e-mail:
For additional commands, e-mail:

View raw message