0

When I am parsing HttpServletRequest to get form data using ServletFileUpload class.

I'm unable to get special characters as it's posted in Form data.

Sample Code:

List<FileItem> fileItemList = null;
ServletFileUpload upload = new ServletFileUpload(new DiskFileItemFactory());
try {
       fileItemList = upload.parseRequest(req);
    } catch (FileUploadException e) {
    e.printStackTrace();
    } 
 if (!isEmpty(fileItemList)) {
    for (FileItem fileItem : fileItemList) {
          if (fileItem.isFormField()) {
        System.out.println("Key: "+fileItem.getFieldName()+" :: Value: "+ fileItem.getString());
       }
 }

Form input text: “I’m coming up with a music band titled ‘Lalisom – The Lal Effect’, with Ratheesh Vega

Those special characters are not returning as it's, instead of it's show some un-identified symbols.

Have tried the below solutions, nothing worked out:

How to get UTF-8 working in Java webapps?

http://mail-archives.apache.org/mod_mbox/commons-user/200602.mbox/%3CE95FD77F0171D14DA1EEE0A785E92B43DA2AB4@mail3.basistech.net%3E

http://www.coderanch.com/t/527577/Servlets/java/Uploading-file-special-characters

http://commons.apache.org/proper/commons-fileupload/streaming.html

My web.xml for Spring project:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    version="2.5">

    <display-name>Generic Web Service</display-name>

    <servlet>
        <servlet-name>services</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>

        <!-- Added by Premananda for getting Osgi Bundle Context -->
        <!--<init-param>
            <param-name>contextClass</param-name>
            <param-value>com.springsource.server.web.dm.ServerOsgiBundleXmlWebApplicationContext</param-value>
        </init-param>-->

        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:springconfig.xml</param-value>
        </init-param>
    </servlet>

    <servlet-mapping>
        <servlet-name>services</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>

    <!--CharsetFilter start--> 

 <filter>
    <filter-name>encoding-filter</filter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</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>FALSE</param-value>
    </init-param>
 </filter>
 <filter-mapping>
    <filter-name>encoding-filter</filter-name>
    <url-pattern>/*</url-pattern>
 </filter-mapping>

  <!--CharsetFilter end-->

</web-app>

Also find the server.xml of tomcat-7:

<?xml version='1.0' encoding='utf-8'?>

<Server port="8005" shutdown="SHUTDOWN">
  <!-- Security listener. Documentation at /docs/config/listeners.html
  <Listener className="org.apache.catalina.security.SecurityListener" />
  -->
  <!--APR library loader. Documentation at /docs/apr.html -->
  <Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" />
  <!--Initialize Jasper prior to webapps are loaded. Documentation at /docs/jasper-howto.html -->
  <Listener className="org.apache.catalina.core.JasperListener" />
  <!-- Prevent memory leaks due to use of particular java/javax APIs-->
  <Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" />
  <Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" />
  <Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener" />

  <!-- Global JNDI resources
       Documentation at /docs/jndi-resources-howto.html
  -->
  <GlobalNamingResources>
    <!-- Editable user database that can also be used by
         UserDatabaseRealm to authenticate users
    -->
    <Resource name="UserDatabase" auth="Container"
              type="org.apache.catalina.UserDatabase"
              description="User database that can be updated and saved"
              factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
              pathname="conf/tomcat-users.xml" />
  </GlobalNamingResources>

  <Service name="Catalina">
  <Connector port="80" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" URIEncoding="UTF-8" />

  <Connector port="8009" protocol="AJP/1.3" redirectPort="8443" URIEncoding="UTF-8" /> 

  <Engine name="Catalina" defaultHost="localhost">
  <Realm className="org.apache.catalina.realm.LockOutRealm">
    <Realm className="org.apache.catalina.realm.UserDatabaseRealm"
               resourceName="UserDatabase"/>
  </Realm>

  <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
               prefix="localhost_access_log." suffix=".txt"
               pattern="%h %l %u %t &quot;%r&quot; %s %b" />

      </Host>
    </Engine>
  </Service>
</Server>

Is there any advice on how to fix it. Thanks in advance.

Note:- Issue occurs only when form post with binary upload.

Community
  • 1
  • 1
Ramu
  • 79
  • 2
  • 10
  • The first link you give is really complete and should work. Please show your web.xml file and the jsp (or HTML) containing the form (only parts relevant to charset and form tag) – Serge Ballesta Nov 06 '14 at 16:25
  • Hi Serge, Find my spring project web.xml attached with the same query. Still i'm failing to get the proper symbols! Thanks for ur reply. – Ramu Nov 10 '14 at 09:29
  • @Durai Can you also post the content for your tomcat's server.xml – shikjohari Nov 10 '14 at 09:31
  • @shikjohari Find the server.xml with my edited query! – Ramu Nov 10 '14 at 09:40
  • I really need to see your form tag. GET and POST do not work the same for non ASCII characters. – Serge Ballesta Nov 10 '14 at 09:50
  • @SergeBallesta We are using ExtJS framework for form handling. In binary form post, content-type used as content-type=multipart/form-data in headers by default. – Ramu Nov 10 '14 at 10:10
  • Please show UTF codes for symbols you try to send, and code that you get in java. – Serge Ballesta Nov 10 '14 at 10:19
  • Form header setup using ExtJS: 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8' and receiving character encoding as UTF-8 in Java. – Ramu Nov 10 '14 at 10:23

1 Answers1

0

Try calling

setHeaderEncoding("iso-8859-2");

Hope this will work.

shikjohari
  • 2,278
  • 11
  • 23