我有以下代码:
@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>
弹簧的web-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
方法GET与POST相对应正确(HTTP状态405 - 不支持请求方法'POST')。我敢肯定 - 配置有问题。 那么,我的错误在哪里。请帮忙。
答案 0 :(得分:3)
问题解决了。它是CSRF令牌。更确切地说:
<form method="POST"
enctype="multipart/form-data"
action="doUpload?${_csrf.parameterName}=${_csrf.token}">
...
<input type="hidden"
name="${_csrf.parameterName}"
value="${_csrf.token}" />
</form>
中的更多信息,请参阅
答案 1 :(得分:2)
可以试一试吗?这就是我在控制器上设置方法的方法。
@RequestMapping(value = "/doUpload", method = RequestMethod.POST)
public String handleFileUpload(MultipartFile uploadedFile, HttpServletRequest request) throws Exception {
int a = 5; //breakpoint is
我不确定CommonsMultipartFile是做什么的。我认为你不需要@RequestParam。
我希望它有所帮助。
更新 - 我认为问题在于价值&#39;按钮上的attr。没有匹配的控制器接受参数&#39;上传&#39;。
答案 2 :(得分:2)
您正在从按钮提交值,但不在控制器方法上接受它。
答案 3 :(得分:0)
试一试 - 使用@RequestBody代替@RequestParam。