使用form表单向接收PUT
的api提交文件结果方案
表单提供的方法为 POST
和 GET
,可以在其中埋一个隐藏的 input
标签,表示方法提供给Spring处理
1
| <input type="hidden" name="_method" value="PUT"/>
|
在Spring的主配置文件 applicationContext.xml
(或别的文件)中添加配置处理类
1 2 3 4 5 6
| <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <property name="maxUploadSize" value="20000000"/> <property name="defaultEncoding" value="UTF-8"/> </bean>
|
之后在 web.xml
中添加过滤器与监听器配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| <filter> <filter-name>MutipartResolver</filter-name> <filter-class>org.springframework.web.multipart.support.MultipartFilter</filter-class> <init-param> <param-name>multipartResolverBeanName</param-name> <param-value>multipartResolver</param-value> </init-param> </filter> <filter-mapping> <filter-name>MutipartResolver</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
<filter> <filter-name>RestfulSupportFilter</filter-name> <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class> </filter> <filter-mapping> <filter-name>RestfulSupportFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
|
1 2 3 4 5 6 7 8
| <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <context-param> <param-name>contextConfigLocation</param-name>
<param-value>classpath:pers/schwarzeni/spring/applicationContext.xml</param-value> </context-param>
|
这个的作用是将 CommonsMultipartResolver
bean加入到 WebApplicationContext
中的 ROOT
下