Announcement Announcement Module
Collapse
No announcement yet.
UTF-8 encoding - resolved Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • UTF-8 encoding - resolved

    When you build UTF-8 project, there are many small things that you have to care about. I workd on a project that needs to take care of UTF-8 for all countries encoding. It wasn't always easy to find solutions for all cornor cases. Below is a list of things you need to worry about if you are working on UTF-8 encoding project.

    1. Your jsp:
    <%@page pageEncoding="UTF-8" contentType="text/html; charset=UTF-8"%>
    2. Your html:
    <meta http-equiv="content-type" content="text/html; charset=UTF-8" http-equiv="language" />
    3. Your java code:
    request.setCharacterEncoding("UTF-8");

    4. tomcat server.xml setting: <Connector port="8080" maxHttpHeaderSize="8192" URIEncoding="UTF-8"...../>

    5. If you are using spring frame work, set the following in web.xml, it handles POST requests
    <filter>
    <filter-name>CharacterEncodingFilter</filter-name>
    <filter-class>org.springframework.web.filter.CharacterEnco dingFilter</filter-class>
    <init-param>
    <param-name>encoding</param-name>
    <param-value>UTF-8</param-value>
    </init-param>
    <init-param>
    <param-name>forceEncoding</param-name>
    <param-value>true</param-value>
    </init-param>
    </filter>


    6. If you are using spring frame work, set the following in your project-servlet.xml. This will take care of file uploading.
    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.C ommonsMultipartResolver"
    lazy-init="true">
    <property name="maxUploadSize">
    <value>157286400</value>
    </property>
    <property name="defaultEncoding">
    <value>UTF-8</value>
    </property>
    </bean>

    7. If your are getting url appending utf-8 params, you need to do the following.
    byte[] file = fileValue.getBytes("UTF-8"); //toString later.

    8. If you are reading from file system in stream
    new FileInputStream(fullFileName), "UTF-8");
    If you are reading from file system in String
    String myFileString = new String(stringFromFileSystem.getBytes(), "UTF-8");

    9. If you are writting to file system
    new FileOutputStream(fullFilePath), "UTF-8");

    10. If you get a bug saying user prompt window can't recognize local encoding, the only answer for this one is:
    The user needs to turn their IE/encoding/local encoding on. E.g. encoding/Chinse simplified

    11. If you are doing your junit test and needs to write unicode in your IDE, you need to config this in your IED, suppose it is eclipse:
    window/preference/General/Workspace/Ohter/UTF-8
Working...
X