4 Replies Latest reply on Oct 8, 2015 5:07 PM by rsoika

    @WebServlet - encoding - @MultipartConfig?

    rsoika

      Hi,

      I have a strange problem with a WebServlet running in Wildfly 8 and 9.

      The servlet is used to extract multipart fileupload requests. The servlet works well in general. It extracts the file content and stores the file in a session parameter. (You can see the sources here.)

      The problem is the extraction of the filename from the parts of a mulitpart http request. In case the filename contains german umlaute (ä,ü,ö...) the filename in the header of the corresponding javax.servlet.http.Part has the wrong encoding.

      I checked this with firebug and the name is send form the browser with UTF-8 encoding. But my servlet did not receife this encoding and it looks like the encoding of the header has change. 

      Is there a known bug with the transmission of multiparts in a servlet or did I missed a configuration isseue?

      The same servlet runs well in GlassFish3

       

      Thanks for any hints helping me to find a soution

       

      ====

      Ralph

        • 1. Re: @WebServlet - encoding - @MultipartConfig?
          rsoika

          I found this thread Rich:fileUpload and character encoding describing the same problem.

          Currently I am running on Wildfly 9.0.1 And adding

          <default-encoding>UTF-8</default-encoding>

          to jboss-web.xml did not solve the problem.

          Only translating the filename with:


          fileName = new String(fileName.getBytes("iso-8859-1"), "utf-8"); 


          did result in the correct format. But I don't think that this is a platform neutral solution? :-/

           

          ===
          Ralph

          • 2. Re: @WebServlet - encoding - @MultipartConfig?
            rsoika

            I now try to solve the problem by guessing the ISO-8859-1 encoding

             

            byte fileNameISOBytes[] = fileName.getBytes("iso-8859-1");
              String fileNameUTF8 = new String(fileNameISOBytes, "UTF-8");
              if (fileName.length() != fileNameUTF8.length()) {
              // convert to utf-8
              logger.fine("filename seems to be ISO-8859-1 encoded");
              fileName = new String(fileName.getBytes("iso-8859-1"),
              "utf-8");
              }
            

             

            A little bit ugly but it works on Wildfly and GlassFish.

            • 3. Re: @WebServlet - encoding - @MultipartConfig?
              rouvas

              Try to set this:

               

              request.setCharacterEncoding("UTF8");

               

              in your servlet before actual processing of the request.

              • 4. Re: @WebServlet - encoding - @MultipartConfig?
                rsoika

                setting

                request.setCharacterEncoding("UTF8"); 

                was my first try. But this have had no effect. The problem is that the header is receifed and processed by undertow in ISO-8859-1 encoding instead of UTF-8, as expected.