上传文件:不支持请求方式'POST'

upload file: Request method 'POST' not supported

我有以下代码:

@Controller
public class FileUploadController {

@Autowired
private AttachmentsToSendJDBCTemplate attachmentsToSendJDBCTemplate;

@RequestMapping(value = "/upload", method = RequestMethod.GET)
public String showUploadForm(HttpServletRequest request) {
    return "upload";
}

@RequestMapping(value = "/doUpload", method = RequestMethod.POST)
public String handleFileUpload(HttpServletRequest request,
@RequestParam CommonsMultipartFile[] fileUpload) throws Exception { 
int a = 5; //breakpoint is here;

upload.jsp:

<form method="post" action="doUpload" enctype="multipart/form-data">
    <table border="0">
        <tr>
            <td><input type="file" name="fileUpload" size="50" /></td>
        </tr>           
        <tr>
            <td colspan="2" align="center"><input type="submit" value="Upload" /></td>
        </tr>
    </table>
</form>

web.xml:

<servlet>
    <servlet-name>dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            /WEB-INF/application-context.xml,
            /WEB-INF/spring-database.xml,
            /WEB-INF/spring-security.xml,
            /WEB-INF/spring-web-config.xml,
            /WEB-INF/mail-service.xml
        </param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
        /WEB-INF/application-context.xml,
        /WEB-INF/spring-database.xml,
        /WEB-INF/spring-security.xml,
        /WEB-INF/spring-web-config.xml,
        /WEB-INF/mail-service.xml
    </param-value>
</context-param>

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

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

spring-网络-config.xml:

<context:component-scan base-package="com.github.fedorchuck.morshinska" />

<bean id="viewResolver"
      class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/" />
    <property name="suffix" value=".jsp" />
</bean>

<bean id="multipartResolver"
      class="org.springframework.web.multipart.commons.CommonsMultipartResolver"/>

<mvc:resources mapping="/resources/**" location="/resources/" cache-period="31556926"/>

<mvc:annotation-driven />

申请-context.xml:

<bean id="springSecurityFilterChain" class="org.springframework.web.filter.DelegatingFilterProxy"/>

<bean id="userDetailsService" class="com.github.fedorchuck.morshinska.service.account.AccountDetailsServiceImpl"/>

<bean class="org.springframework.security.authentication.encoding.Md5PasswordEncoder" id="passwordEncoder"/>

<bean id="emailJDBCTemplate" class="com.github.fedorchuck.morshinska.dao.impl.EmailJDBCTemplate">
    <property name="dataSource"  ref="dataSource" />
</bean>

<bean id="usersJDBCTemplate" class="com.github.fedorchuck.morshinska.dao.impl.UsersJDBCTemplate">
    <property name="dataSource"  ref="dataSource" />
</bean>

<bean id="attachmentsToSendJDBCTemplate" class="com.github.fedorchuck.morshinska.dao.impl.AttachmentsToSendJDBCTemplate">
    <property name="dataSource"  ref="dataSource" />
</bean>

并记录:

org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping register
INFO: Mapped "{[/upload],methods=[GET]}" onto public java.lang.String com.github.fedorchuck.morshinska.web.controller.FileUploadController.showUploadForm(javax.servlet.http.HttpServletRequest)
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping register
INFO: Mapped "{[/doUpload],methods=[POST]}" onto public java.lang.String com.github.fedorchuck.morshinska.web.controller.FileUploadController.handleFileUpload(javax.servlet.http.HttpServletRequest,org.springframework.web.multipart.commons.CommonsMultipartFile[]) throws java.lang.Exception
...
org.springframework.web.servlet.PageNotFound handleHttpRequestMethodNotSupported
  WARNING: Request method 'POST' not supported
org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver logException
  WARNING: Handler execution resulted in exception: Request method 'POST' not supported

与 POST 相比,GET 方法工作正常(HTTP 状态 405 - 不支持请求方法 'POST')。我确定 - 配置有问题。 那么,我的错误在哪里。请帮忙。

可以试一下吗?这就是我在控制器上设置方法的方式。

@RequestMapping(value = "/doUpload", method = RequestMethod.POST)
public String handleFileUpload(MultipartFile uploadedFile, HttpServletRequest request) throws Exception { 
int a = 5; //breakpoint is 

我不确定 CommonsMultipartFile 的作用。我认为你不需要@RequestParam。

希望对您有所帮助。

更新 - 我认为问题是按钮上的 'value' 属性。没有接受参数 'Upload'.

的匹配控制器

试一试 - 使用@RequestBody 代替@RequestParam。

您正在从按钮提交一个值,但没有在控制器方法上接受它。

问题已解决。这是 CSRF 令牌。更准确地说:

<form method="POST" 
      enctype="multipart/form-data" 
      action="doUpload?${_csrf.parameterName}=${_csrf.token}">
      ...
      <input type="hidden" 
             name="${_csrf.parameterName}" 
             value="${_csrf.token}" />
</form>

有关 docs.spring.io

中的更多信息章节

我遇到了这个问题,花了好几个小时才得到答案。以下是您在上传多部分文件时是否收到 405(不支持请求方法 'POST')需要检查的所有步骤!

  1. Chek _csrfenctype 值在 .jsp 文件中 html 表格标签 .
  2. 确保在 spring-servlet.xml:
    中有 multipartResolver bean <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"/,如果在创建这个 bean 时出现异常,add this dependency to your pom
  3. 检查处理请求的控制器方法的注解,它应该是这样的:
    @RequestMapping(value = "/requestpath", method = RequestMethod.POST, consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    如果你有额外的字段,这个
    @RequestMapping(method = RequestMethod.POST, consumes = {MediaType.MULTIPART_FORM_DATA_VALUE, MediaType.APPLICATION_FORM_URLENCODED_VALUE})

总结一下:.jsp中的_csrf和enctype,spring-servlet.xml中的bean,controller中的request consumer