简体   繁体   中英

Encoding of a multipart/form-data in Struts 1

I have a very strange situation with a Struts web application:

I have a page with a multipart/form-data enctype form. The page is in ISO-8859-1 and I have 2 different situations depending on the tomcat environment:

  • The development environment is a Windows machine with the windows default encoding.
  • The production environment is a Linux machine with UTF-8 encoding.

    :$ echo $LANG
    :$ es_ES.UTF-8

In windows environment, when the form is submited all parameters are correctly encoded.

In linux environment, when the form is submited, Struts retrieves the parameters with a wrong encoding (symbols like "????" instead of "áéíóú").

The rest of POST forms of the web application are working correctly in both environments. The problem is only with multipart forms.

Is it possible to configure on Tomcat the encoding of the multipart requests?

I suspect that the problem could be that in the multipart form the characters are not encoded and tomcat does not know the encoding of the request, so it tries to decode them with the encoding of the OS.

So, a solution that worked for me was to develop a filter to set the encoding of the request to ISO-8895-1.

The filter only sets the encoding:

    public void doFilter(ServletRequest request, ServletResponse aResponse,
            FilterChain filterChain) throws IOException, ServletException {

            request.setCharacterEncoding("ISO-8859-1");
            filterChain.doFilter(aRequest, aResponse);
    }

With this filter, it is working on both environments.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM