解析HttpServletRequest时的特殊字符问题

时间:2014-11-06 14:36:05

标签: java servlets special-characters

当我解析HttpServletRequest以使用ServletFileUpload类获取表单数据时。

我无法获得特殊字符,因为它已发布在表单数据中。

示例代码:

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());
       }
 }

表格输入文字:“我想出一个名为'Lalisom - The Lal Effect'的音乐乐队,与Ratheesh Vega一起

这些特殊字符没有按原样返回,而是显示一些未识别的符号。

尝试过以下解决方案,没有成功:

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

我的Spring项目的web.xml:

<?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>

还可以找到tomcat-7的server.xml:

<?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>

是否有任何关于如何修复它的建议。提前谢谢。

注意: - 仅在使用二进制上传的表单发布时才会出现问题。

1 个答案:

答案 0 :(得分:0)

尝试拨打

setHeaderEncoding("iso-8859-2");

希望这会奏效。