【问题标题】:Spring MVC Error 405 Request method 'POST' not supported when uploading a file上传文件时不支持 Spring MVC 错误 405 请求方法“POST”
【发布时间】:2014-06-17 00:21:55
【问题描述】:

我想我已经看过网络上关于这个主题的所有帖子,但我无法纠正这个错误:(

我有一个使用 Spring Security 和 Spring Mvc 的 Web 应用程序,我想创建一个表单来上传图像(您必须登录才能执行此操作),但无论我用我在论坛上找到的内容以何种方式扭曲我的代码,我有一个上传文件时不支持错误 405 请求方法“POST”

这是我的 applicationContext.xml :

 <beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns:context="http://www.springframework.org/schema/context"
   xsi:schemaLocation="http://www.springframework.org/schema/beans 
                       http://www.springframework.org/schema/beans/spring-beans.xsd
                       http://www.springframework.org/schema/context
                       http://www.springframework.org/schema/context/spring-context.xsd">


<context:component-scan base-package="com.meltdown.*" />
<context:annotation-config />

<bean id="userDAO" class="com.meltdown.bo.users.infra.impl.JdbcUserDAO">
    <property name="dataSource" ref="dataSource" />
 </bean>
<bean id="userService" class="com.meltdown.bo.users.application.service.impl.StandardUserService" />

 <bean id="barDAO" class="com.meltdown.bo.bars.infra.impl.JdbcBarDAO">
    <property name="dataSource" ref="dataSource" />
 </bean>
<bean id="barService" class="com.meltdown.bo.bars.application.service.impl.StandardBarService" />

 <bean id="newsDAO" class="com.meltdown.bo.news.infra.impl.JdbcNewsDAO">
    <property name="dataSource" ref="dataSource" />
 </bean>
<bean id="newsService" class="com.meltdown.bo.news.application.service.impl.StandardNewsService" />

<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- one of the properties available; the maximum file size in bytes -->
<property name="maxUploadSize" value="100000"/>
</bean>
</beans>

我的控制器:

@Controller
public class FileUploadController {
@RequestMapping(value="/bo/uploadImage", method = RequestMethod.GET)
public String uploadImage() {

    return "bo_uploadimage";
}

@RequestMapping(value="/bo/uploadImage", method = RequestMethod.POST)
public String uploadImage(@RequestParam(value = "file")FileUploadBean file, BindException errors, Map<String, Object> model) {

    System.out.println("#############################" + file);

    return "bo_uploadimage";
}
}



public class FileUploadBean{

private byte[] file;

public void setFile(byte[] file) {
    this.file = file;
}

public byte[] getFile() {
    return file;
}
}

jsp:

<html>
<head>
    <title>Upload a file please</title>
</head>
<body>
    <h1>Please upload a file</h1>
    <form method="post" action="/meltdown/bo/uploadImage" enctype="multipart/form-data">
        <input type="file" name="file"/>
        <input type="submit"/>
    </form>
</body>
</html>

我认为问题出在我的控制器上,可能是因为我将 Spring 4 注释与 Spring3 conf 混淆了?

感谢您的帮助!!

编辑 mvc-dispatcher-servlet.xml

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
    http://www.springframework.org/schema/beans     
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-3.0.xsd
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

<context:component-scan base-package="com.meltdown.*" />

<mvc:annotation-driven />

<bean id="messageSource"
     class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
    <property name="basename" value="classpath:messages" />
    <property name="defaultEncoding" value="ISO-8859-1" />
</bean>

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

<bean id="multipartResolver"  class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- one of the properties available; the maximum file size in bytes -->
    <property name="maxUploadSize" value="100000"/>
</bean>
</beans>

【问题讨论】:

  • 你的控制器配置在哪里?
  • 除了web.xml我没有其他配置。 @Controller 注释完成我猜的其余部分
  • 我的错,我有一个 mvc-context-servlet.xml
  • 从阅读以下内容开始:stackoverflow.com/questions/11708967/…。您的配置具有非常冗余的元素。然后将你的日志级别转为调试并检查 Spring 吐出的内容。
  • 好吧,也许我用 slf4j 得到了一些东西:20:58:38,370 DEBUG CsrfFilter:95 - Invalid CSRF token found for http://localhost:8080/meltdown/bo/createnews 我要调查这一点......

标签: java spring spring-mvc file-upload spring-security


【解决方案1】:

好的,我终于找到了问题。

首先,我使用了几乎所有教程中提到的 MultipartFile,并且我使用了 @ModelAttribute 将此 MultipartFile 映射到我的表单。但这并不是真正的问题:我只是清理了实现以使其更加标准。

然后,我在 DEBUG 日志中发现了一些错误:

20:58:38,370 DEBUG CsrfFilter:95 - Invalid CSRF token found for http://localhost:8080/meltdown/bo/createnews

我使用 spring 推荐来纠正它:(参见 spring security csrf doc)

将 CSRF 保护与 multipart/form-data 结合使用有两种选择。每个选项都有其权衡。

在 Spring Security 之前放置 MultipartFilter

在操作中包含 CSRF 令牌

我使用第二个选项并将 ?${_csrf.parameterName}=${_csrf.token} 放在表单操作网址的末尾。

它有效,但我必须挖掘一下所有这些东西下面的内容......并检查真正的内容以及是否需要 csrf。

感谢大家的帮助

【讨论】:

  • 太棒了……这个真的帮助了我陷入困境。安全 csrf
猜你喜欢
  • 2014-09-18
  • 2020-08-12
  • 2020-07-24
  • 2016-06-16
  • 2014-06-10
  • 2018-04-13
  • 2012-06-24
  • 2015-04-09
  • 2016-03-29
相关资源
最近更新 更多