简体   繁体   English

解析HttpServletRequest时出现特殊字符问题

[英]Special character issue when parsing HttpServletRequest

When I am parsing HttpServletRequest to get form data using ServletFileUpload class. 当我解析HttpServletRequest以使用ServletFileUpload类获取表单数据时。

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 表单输入文本: “我要与Ratheesh Vega一起创作一个名为'Lalisom – Lal Effect'的乐队

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? 如何使UTF-8在Java Webapps中工作?

http://mail-archives.apache.org/mod_mbox/commons-user/200602.mbox/%3CE95FD77F0171D14DA1EEE0A785E92B43DA2AB4@mail3.basistech.net%3E 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://www.coderanch.com/t/527577/Servlets/java/Uploading-file-special-characters

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

My web.xml for Spring project: 我的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>

Also find the server.xml of tomcat-7: 还要找到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>

Is there any advice on how to fix it. 关于如何解决它有什么建议。 Thanks in advance. 提前致谢。

Note:- Issue occurs only when form post with binary upload. 注意:-仅在带有二进制上传的表单发布时才会发生问题。

Try calling 尝试致电

setHeaderEncoding("iso-8859-2");

Hope this will work. 希望这会起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM