我有一个非常简单的 JSP/Servlet 3.0/Spring MVC 3.1 应用程序。
在我的一个页面上,我有多个表格。其中一种形式允许用户上传文件,因此配置为enctype="multipart/form-data"
. 我使用自 Servlet 3.0 以来可用的元素配置了 web.xml 文件中的分段上传multipart-config
,并结合了<bean id="multipartResolver" class="org.springframework.web.multipart.support.StandardServletMultipartResolver"/>
我的 spring 配置。
我也org.springframework.web.filter.CharacterEncodingFilter
配置了 Spring。
我遇到的问题是我找不到将 StandardServletMultipartResolver 的默认编码设置为 UTF-8 的方法,这通常会导致多部分表单中文本字段的内容全部乱码。
有没有什么办法解决这一问题?
提前致谢。
web.xml 配置:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
<display-name>foo-web</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
WEB-INF\applicationContext.xml
</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>foo</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>0</load-on-startup>
<multipart-config>
<max-file-size>52428800</max-file-size>
<file-size-threshold>5242880</file-size-threshold>
</multipart-config>
</servlet>
<servlet-mapping>
<servlet-name>foo</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<filter>
<filter-name>CharacterEncodingFilter</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>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter>
<filter-name>springSecurityFilterChain</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>login</welcome-file>
</welcome-file-list>