学习springMVC文件上传的时候,选择了文件提交以后报错404
jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>文件上传</title>
</head>
<body>
<h3>文件上传</h3>
<form action="/User/fileUpload" method="post" enctype="multipart/form-data">
<input type="file" name="upload"><br/>
<input type="submit" value="上传">
</form>
</body>
</html>
spring-MVC配置如下
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
https://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd">
<!--开启注解扫描-->
<context:component-scan base-package="com.ryn"/>
<!--配置视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!--配置解析器-->
<property name="suffix" value=".jsp"/>
<!--配置目录-->
<property name="prefix" value="/WEB-INF/pages/"/>
</bean>
<!--配置静态资源-->
<mvc:resources location="/upload/" mapping="/upload/**" />
<mvc:resources mapping="/js/**" location="js/"/>
<!--配置文件解析器-->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="defaultEncoding" value="utf-8"/>
<property name="maxUploadSize" value="10485760"/>
</bean>
<!--开启springMVC框架注解的支持-->
<mvc:annotation-driven/>
</beans>
web.xml配置
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>Archetype Created Web Application</display-name>
<!--配置前端控制器-->
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
uploadController
/**
* 文件上传
* @return
*/
@RequestMapping("/fileUpload")
public String fileUpload(HttpServletRequest request, @RequestParam("upload") MultipartFile upload) throws Exception{
System.out.println("文件上传");
//指定上传位置
String path = request.getSession().getServletContext().getRealPath("/upload/");
//判断文件夹是否存在
File file = new File(path);
if (!file.exists()){
//若不存在,则创建文件夹
file.mkdirs();
}
String filename = upload.getOriginalFilename();
//使用write方法上传文件
String uuid = UUID.randomUUID().toString().replaceAll("-", "").toUpperCase();
filename = uuid + "_" + filename;
upload.transferTo(new File(path,filename));
return "success";
}
报错信息如下
求各位大佬帮我看看问题到底出在哪了!!!弄了半个小时不知道怎么回事