I think you are correct. When I was looking at the packets and seeing two characters, it is actually the characters that are equal to the 2 bytes that make up the single UTF-8 character. I thought the browser was somehow not correctly encoding my data, because it was turning 1 character into 2 characters, but actually it is UTF-8 encoding my character correctly. So I think if I use something to read the data and convert it from UTF-8 to Unicode, I will get the correct data on the server.

So from reading the documentation about FileUpload, that seems to be the way to go, but now my question is how to integrate FileUpload with struts? My thought would be to call a method to populate an ActionForm in the beginning of my action, and then use that ActionForm instead of the one I get from the requestProcessor. So like this:

     public ActionForward execute(
             ActionMapping mapping,
             ActionForm pform,
             HttpServletRequest request,
             HttpServletResponse response)
             throws Exception {
         TestActionForm form = getFormUsingFileUpload(request);
         log.info("The value is: "+form.getTest());
         return null;
    }

Is this how others have used Jakarta Commons FileUpload with Struts, or is there a better way?


Jason Lea wrote:


From what I can see there Resin is expecting UTF-8 for any paramters passed to it, and decoding it correctly. However multipart/form-data is treated differently as the data is not passed as normal parameters so the request.getParameter() cannot be used here (and servlet filters that set the request encoding won't help either).

You normally have to use something like the FileUpload component to extract form fields and files from the request. This component is not going to know about the character encoding you have given to resin, so it will use the default which is probably US-ASCII. With UTF-8 a single character can be rendered as 1, 2 or 3 bytes. When decoding a UTF-8 string the decoder will combine the 1,2 or 3 byte combinations into 1 Unicode character. When UTF-8 is not used to decode the string you will see the individual bytes.

Looking here (the jakarta apache FileUpload component):
http://jakarta.apache.org/commons/fileupload/apidocs/org/apache/commons/fileupload/FileUploadBase.html



They have a setHeaderEncoding() method which I assume will deal with this problem (I haven't tested this so I don't know). Are you using a file upload component?



Paul Barry wrote:


I am using Struts 1.1 in an application that needs to support the UTF-8 character set. I am using Resin 2.1.10 with character-encoding="UTF-8", and on most of my forms this seems to work just fine. I am having problems with forms that have to use the multipart/form-data enctype for handling uploading files. If I print out the value of a text element in an html:form where the enctype is not set at all (which ends up using application/x-www-form-urlencoded), using UTF-8 characters works fine. This is what I get:

INFO - test.TestAction - The value is: š

Here is what the actual HTTP request that gets sent to the server looks like:

--- Start HTTP Request -----------------------------------------------------
POST /testForm.do HTTP/1.1
Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, */*
Referer: http://pbdesktop/test.do
Accept-Language: en-us
Content-Type: application/x-www-form-urlencoded
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)
Host: pbdesktop
Content-Length: 11
Connection: Keep-Alive
Cache-Control: no-cache
Cookie: SERVER=op; locale=en_US; JSESSIONID=aoUCARQpqsLd


test=%C3%AD
--- End HTTP Request ------------------------------------------------------


But if I modify my html:form to use enctype="multipart/form-data", I get this:

INFO - test.TestAction - The value is: A§

And the HTTP request looks like this:

--- Start HTTP Request -----------------------------------------------------
POST /testForm.do HTTP/1.1
Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, */*
Referer: http://pbdesktop/test.do
Accept-Language: en-us
Content-Type: multipart/form-data; boundary=---------------------------7d319628600e4
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)
Host: pbdesktop
Content-Length: 141
Connection: Keep-Alive
Cache-Control: no-cache
Cookie: SERVER=op; locale=en_US; JSESSIONID=aoUCARQpqsLd


-----------------------------7d319628600e4
Content-Disposition: form-data; name="test"

í
-----------------------------7d319628600e4-
--- End HTTP Request ------------------------------------------------------


It looks as if the character is already messed up before it even gets to the servlet container. There are messages in the mailing list archive that discuss this problem, but I didn't see a solution. What is the best way to handle UTF-8 characters in a multipart/form-data encoded form?

Here is the code that I am testing with:

/test/test.jsp:
<%@ taglib uri="WEB-INF/taglib/struts-html.tld" prefix="html" %>
<%@ taglib uri="WEB-INF/taglib/struts-bean.tld" prefix="bean" %>

<html:html>
  <body>
    <html:form action="testForm.do" enctype="multipart/form-data">
      <html:text property="test" />
      <html:submit />
    </html:form>
  </body>
</html:html>

Relavent parts of struts-config.xml:
<struts-config>

  <form-beans>
    <form-bean name="testForm" type="test.TestActionForm" />
  </form-beans>

<action-mappings>
<action path="/test" type="org.apache.struts.actions.ForwardAction" parameter="/test/test.jsp" />
<action path="/testForm" type="test.TestAction" name="testForm" input="/test.do" scope="request" />
</action-mappings>


<controller contentType="text/html;charset=UTF-8" />

<struts-config/>

test.TestAction:
package test;

import javax.servlet.http.*;
import org.apache.commons.logging.*;
import org.apache.struts.action.*;

public class TestAction extends Action {
private static final Log log = LogFactory.getLog(TestAction.class);
public ActionForward execute(
ActionMapping mapping,
ActionForm pform,
HttpServletRequest request,
HttpServletResponse response)
throws Exception {
TestActionForm form = (TestActionForm)pform;
log.info("The value is: "+form.getTest());
return null;
}
}


test.TestActionForm:
package test;

import org.apache.struts.action.ActionForm;

public class TestActionForm extends ActionForm {
    private String test;
    public String getTest() { return test;     }
    public void setTest(String string) { test = string; }
}


--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]








---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Reply via email to