gpt4 book ai didi

java - 上传文件 : Request method 'POST' not supported

转载 作者:行者123 更新时间:2023-12-02 07:22:50 25 4
gpt4 key购买 nike

我有以下代码:

@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;

上传.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>

spring-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 />

应用程序上下文.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

与 POST 相比,GET 方法工作正常(HTTP 状态 405 - 不支持请求方法“POST”)。我确定 - 配置有问题。那么,我的错误在哪里。请帮忙。

最佳答案

问题解决了。这是 CSRF token 。更准确地说:

<form method="POST" 
enctype="multipart/form-data"
action="doUpload?${_csrf.parameterName}=${_csrf.token}">
...
<input type="hidden"
name="${_csrf.parameterName}"
value="${_csrf.token}" />
</form>

有关更多信息,请参阅 docs.spring.io 中的章节

关于java - 上传文件 : Request method 'POST' not supported,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38153219/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com