首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >上传文件时不支持Spring错误405请求方法“POST”

上传文件时不支持Spring错误405请求方法“POST”
EN

Stack Overflow用户
提问于 2014-06-12 17:58:34
回答 1查看 4.3K关注 0票数 1

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

我有一个使用Security和Spring的Web,我想创建一个表单来上传一个图像(您必须被记录下来才能这样做),但是无论我用我在论坛上找到的东西扭曲代码的方式,我有一个错误405请求方法'POST‘在上传文件时不支持

这是我的applicationContext.xml:

代码语言:javascript
复制
 <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>

我的财务主任:

代码语言:javascript
复制
@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:

代码语言:javascript
复制
<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>

我认为这个问题来自于我的控制器,也许是因为我把Spring4注释与Spring3 conf混淆了?

谢谢你的帮助!!

编辑mvc-dispatcher-servlet.xml

代码语言:javascript
复制
<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>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-06-16 16:21:55

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

首先,我在几乎所有教程中都提到了MultipartFile,我使用@ModelAttribute将这个MultipartFile映射到我的表单中。但这并不是真正的问题:我只是清理了植入,以使它更标准。

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

代码语言:javascript
复制
20:58:38,370 DEBUG CsrfFilter:95 - Invalid CSRF token found for http://localhost:8080/meltdown/bo/createnews

我使用了spring建议来纠正它:(请参阅spring安全csrf文档)

使用多部分/表单数据的CSRF保护有两种选择。每一种选择都有它的权衡。 在春季安全前放置MultipartFilter 包括CSRF令牌的动作

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

它很管用,但我得挖一些东西下面的东西.看看什么才是真正的我是否需要csrf。

谢谢大家的帮助

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24190962

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档