Spring MVC Error 405上传文件时不支持请求方法'POST' [英] Spring MVC Error 405 Request method 'POST' not supported when uploading a file

查看:182
本文介绍了Spring MVC Error 405上传文件时不支持请求方法'POST'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想我已经在网上看过关于这个话题的每一篇文章,但我无法纠正这个错误:(

I think I have seen every post on the web about this topic but I cannot correct that bug :(

我有一个使用Spring Security和Spring Mvc的Web应用程序并且我想创建一个表单来上传图像(你必须记录才能这样做)但是无论我用论坛上的内容扭曲我的代码,我都有一个错误405请求方法'POST'没有上传文件时支持

I have a Web App using Spring Security and Spring Mvc and I want to create a form to upload an image (you have to be logged to do that) but whatever the way I twist my code with what I find on forums, I have an Error 405 Request method 'POST' not supported when uploading a file

这是我的applicationContext.xml:

Here is my 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混合在一起?

I think the problem come from my controler and maybe because I mixed up Spring 4 annotations with Spring3 conf?

感谢您的帮助!!

编辑mvc-dispatcher-servlet.xml

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


推荐答案

好的,我终于发现了问题。

Ok so I finally find out the problem.

首先,我使用了几乎所有教程中提到的MultipartFile,并使用@ModelAttribute将此MultipartFile映射到我的表单。但问题并非如此:我只是清理了这个问题,使其更加标准化。

First, I used MultipartFile as mentionned in almost all tutorials and i used an @ModelAttribute to map this MultipartFile to my form. But it was not really the problem : I just cleaned up the implmentation to make it more standard.

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

Then, I discovered something wrong in the DEBUG logs :

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

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

and i used spring recommendation to correct it : (see spring security csrf doc)


使用CSRF保护和multipart / form-data有两种选择。每个选项都有权衡。

There are two options to using CSRF protection with multipart/form-data. Each option has its tradeoffs.

在Spring Security之前放置MultipartFilter

Placing MultipartFilter before Spring Security

在行动中包含CSRF令牌

Include CSRF token in action

我使用了第二个选项并在结尾处放置了?$ {_ csrf.parameterName} = $ {_ csrf.token} 我的表格的动作网址。

I used second option and placed ?${_csrf.parameterName}=${_csrf.token} at the end of my form's action url.

它有效,但我必须在所有这些东西下挖掘一下...并检查什么是真的,如果我需要csrf。

It works but I have to dig a bit what s under all that stuff... And to check what is really and if I need csrf.

谢谢大家的帮助

这篇关于Spring MVC Error 405上传文件时不支持请求方法'POST'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆