上载文件时不支持Spring MVC错误405请求方法'POST'

易北河

我想我已经在网络上看到过有关该主题的每篇文章,但我无法纠正该错误:(

我有一个使用Spring Security和Spring Mvc的Web App,我想创建一个表单来上传图像(必须登录才能这样做),但是无论我将代码与论坛上找到的内容弄混的方式如何,我都有一个上载文件时不支持错误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>
易北河

好了,我终于找到了问题所在。

首先,我使用了几乎所有教程中都提到的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。

谢谢你们每一个人的帮助

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Spring MVC上传文件-HTTP状态405-不支持请求方法“ POST”

来自分类Dev

Spring MVC HTTP状态405-请求方法'POST'不支持-骨干请求

来自分类Dev

Spring MVC的CURL POST请求给出405方法'POST'不支持

来自分类Dev

HTTP状态405-具有Spring Security的Spring MVC不支持请求方法'POST'

来自分类Dev

Spring MVC-不支持请求方法“ POST”

来自分类Dev

“请求的资源不支持http方法'POST'-405响应

来自分类Dev

为什么在Vuejs中使用Laravel 8进行AJAX POST请求会引发405(不支持方法)错误?

来自分类Dev

HTTP 状态 405 - 来自 angular 的 $http.post 调用不支持请求方法“GET”错误

来自分类Dev

使用Spring Security登录:请求方法'POST'不支持

来自分类Dev

Spring Security 4:不支持请求方法“ POST”

来自分类Dev

Spring Security不支持请求方法“ POST”

来自分类Dev

请求方法'POST'不支持Spring Boot

来自分类Dev

Spring Boot新手-请求方法'POST'不支持

来自分类Dev

上传文件:不支持请求方法“ POST”

来自分类Dev

WebApi 2 Http Post 405“请求的资源不支持http方法'POST'”

来自分类Dev

WebApi 2 Http Post 405“请求的资源不支持http方法'POST'”

来自分类Dev

Web Api MVC 错误:(请求的资源不支持 http 方法“POST”。)

来自分类Dev

MVC-请求的资源不支持http方法“ POST”

来自分类Dev

春季:请求方法'POST'不支持

来自分类Dev

HttpRequestMethodNotSupportedException:不支持请求方法“ POST”

来自分类Dev

不支持 Springboot 请求方法“POST”

来自分类Dev

Spring POST请求不支持的媒体类型415

来自分类Dev

Spring POST请求不支持的媒体类型415

来自分类Dev

IBM Worklight-此URL不支持错误405 HTTP方法POST

来自分类Dev

将 get 请求转发到 post 请求的 jattempting 返回不支持的错误 Spring

来自分类Dev

Spring Boot - 不支持请求方法“POST”(不允许方法)

来自分类Dev

发送多部分格式数据时,请求的资源不支持 http 方法“POST”

来自分类Dev

Web API不支持POST方法

来自分类Dev

此路线不支持POST方法

Related 相关文章

  1. 1

    Spring MVC上传文件-HTTP状态405-不支持请求方法“ POST”

  2. 2

    Spring MVC HTTP状态405-请求方法'POST'不支持-骨干请求

  3. 3

    Spring MVC的CURL POST请求给出405方法'POST'不支持

  4. 4

    HTTP状态405-具有Spring Security的Spring MVC不支持请求方法'POST'

  5. 5

    Spring MVC-不支持请求方法“ POST”

  6. 6

    “请求的资源不支持http方法'POST'-405响应

  7. 7

    为什么在Vuejs中使用Laravel 8进行AJAX POST请求会引发405(不支持方法)错误?

  8. 8

    HTTP 状态 405 - 来自 angular 的 $http.post 调用不支持请求方法“GET”错误

  9. 9

    使用Spring Security登录:请求方法'POST'不支持

  10. 10

    Spring Security 4:不支持请求方法“ POST”

  11. 11

    Spring Security不支持请求方法“ POST”

  12. 12

    请求方法'POST'不支持Spring Boot

  13. 13

    Spring Boot新手-请求方法'POST'不支持

  14. 14

    上传文件:不支持请求方法“ POST”

  15. 15

    WebApi 2 Http Post 405“请求的资源不支持http方法'POST'”

  16. 16

    WebApi 2 Http Post 405“请求的资源不支持http方法'POST'”

  17. 17

    Web Api MVC 错误:(请求的资源不支持 http 方法“POST”。)

  18. 18

    MVC-请求的资源不支持http方法“ POST”

  19. 19

    春季:请求方法'POST'不支持

  20. 20

    HttpRequestMethodNotSupportedException:不支持请求方法“ POST”

  21. 21

    不支持 Springboot 请求方法“POST”

  22. 22

    Spring POST请求不支持的媒体类型415

  23. 23

    Spring POST请求不支持的媒体类型415

  24. 24

    IBM Worklight-此URL不支持错误405 HTTP方法POST

  25. 25

    将 get 请求转发到 post 请求的 jattempting 返回不支持的错误 Spring

  26. 26

    Spring Boot - 不支持请求方法“POST”(不允许方法)

  27. 27

    发送多部分格式数据时,请求的资源不支持 http 方法“POST”

  28. 28

    Web API不支持POST方法

  29. 29

    此路线不支持POST方法

热门标签

归档