Search code examples
javagoogle-app-engineservletsencodingapache-commons-fileupload

How can I find the encoding of a FileItemStream returned by FileItemIterator?


I use Apache Commons FileUpload to receive uploaded files in a Servlet, as described at http://code.google.com/appengine/kb/java.html#fileforms

        ServletFileUpload upload = new ServletFileUpload();

        FileItemIterator iterator = upload.getItemIterator(request);

        while (iterator.hasNext()) {
            FileItemStream item = iterator.next();
            InputStream stream = item.openStream();

            if (!item.isFormField()) {
              System.out.println("Got an uploaded file: " + item.getFieldName()
                        + ", name = " + item.getName() + " type = " +               item.getContentType());

            }

I am not sure if item.getContentType() also will coutain the file encoding for text files which could be different from the request encoding, (for example ISO-8859-1), or if it always only contains the file type only. In my tests I received only "text/plain" but I was expecting "text/plain; Encoding=ISO-8859-1" which has been sent from the client.

Is my understanding correct that the item.getContentType() should include the encoding (if it is sent from client)?


Solution

  • FileItemStream.getContentType() returns whatever was passed from the browser's POST. It could be "text/plain" or it could be "text/plain; Encoding=ISO-8859-1" OR it could be complete garbage. The content type is just a string value you are trusting the browser to give you properly (in other words, don't trust it at all).