从Flash Player接收的参数的字符编码

时间:2013-01-19 15:55:21

标签: flash flex tomcat servlets character-encoding

我解决了Flash Player HttpService发送的参数(来自Spark Form的数据)并在Java Servlet中收到的问题。这是HttpService和Form的客户端代码:

<mx:HTTPService id="submitForm" result="onResultSend(event)" method="POST" url="{EVMServerConnection.SERVER_ADDRESS}/UserSubmitionServlet" useProxy="false" resultFormat="text">
        <mx:request xmlns="">
            <login>{login.text}</login>
            <password>{password.text}</password>
            <email>{email.text}</email>
            <userName>{userName.text}</userName>
            <secondName>{secondName.text}</secondName>
            <gender>{String(gender.selectedItem)}</gender>
            <countryKey>{String(country.selectedItem)}</countryKey>
            <city>{city.text}</city>
            <dateOfBirth>{String(dayOfBirth.selectedItem) + String(monthOfBirth.selectedItem) + String(yearOfBirth.selectedItem)}</dateOfBirth>
        </mx:request> 
    </mx:HTTPService>

servlet方法:

private void registerUser(HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException {
    //request.setCharacterEncoding("UTF-16LE");
    System.out.println(request.getCharacterEncoding());
    String login = request.getParameter("login");
    String password = request.getParameter("password");
    String email = request.getParameter("email");
    //String userName = DataUtils.getUTF8FromUTF16String( request.getParameter("userName") );
    //String userName = request.getParameter("userName");
    byte[] bytes = request.getParameter("userName").getBytes();
    String userName = new String(bytes , "Unicode" );
    System.out.println("UserName: " + userName);
    String secondName = request.getParameter("secondName");
    String gender = request.getParameter("gender");
    boolean isMale = false;
    if (gender != null)
        isMale = !gender.equals("1");
    //String countryKey = request.getParameter("countryKey");
    String city = request.getParameter("city");
    String dateOfBirth = request.getParameter("dateOfBirth");
    Date dateDateOfBirth = null;

当我尝试在控制台上显示userName时,我会得到中文符号,但它应该是波兰语字母。我尝试在新的String()中使用UTF-16,UTF-16LE,UTF-16BE而不是Unicode参数,但结果是相似的。

1 个答案:

答案 0 :(得分:0)

有用的是Apache Tomcat 7提供的课程,我正在使用它。我在我的web.xml中添加了SetCharacterEncodingFilter,现在一切正常,我可以用普通的方式读取参数(通过String name = request.getParameter(“name”))

...
<filter>
    <filter-name>SetCharacterEncoding</filter-name>
    <filter-class>org.apache.catalina.filters.SetCharacterEncodingFilter</filter-class>
    <init-param>
        <param-name>encoding</param-name>
        <param-value>UTF-8</param-value>
    </init-param>
</filter>
<filter-mapping>
    <filter-name>SetCharacterEncoding</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>
...