本人微信公众号"aeolian"~

Restful风格,PUT修改功能请求,表单中存在文件报错-HTTP Status 405 - Request method 'POST' not supported

解决方案配置如下

    
    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> 
          
         <property name="maxUploadSize" value="20000000"/>
         <property name="defaultEncoding" value="utf-8">property>
    bean>

    
    <context-param>
        <param-name>contextConfigLocationparam-name>
        <param-value>classpath:spring/root-context.xmlparam-value>
    context-param>    

    
    <filter>
        <filter-name>MultipartFilterfilter-name>
        <filter-class>org.springframework.web.multipart.support.MultipartFilterfilter-class>
        <init-param>
            <param-name>multipartResolverBeanNameparam-name>
            
            <param-value>multipartResolverparam-value>
        init-param>
    filter>
    <filter-mapping>
        <filter-name>MultipartFilterfilter-name>
        <url-pattern>/*url-pattern>
    filter-mapping>

    
    <filter>
        <filter-name>hiddenHttpMethodFilterfilter-name>
        <filter-class>org.springframework.web.filter.HiddenHttpMethodFilterfilter-class>
    filter>
    <filter-mapping>
        <filter-name>hiddenHttpMethodFilterfilter-name>
        <url-pattern>/*url-pattern>
    filter-mapping>

原理是过滤器是逐个进行的,先用MultipartFilter处理文件,在用HiddenHttpMethodFilter处理form表单中隐藏的name为_method的input。其次MultipartFilter配置的multipartResolverBeanName的值要在contextConfigLocation配置的xml中。

org.springframework.web.multipart.support.MultipartFilter会处理http请求中的文件流,无法解析_method参数。

org.springframework.web.filter.HiddenHttpMethodFilter可以解析form表单中隐藏的name为_method的input。

 

点赞

Leave a Reply

Your email address will not be published. Required fields are marked *