一、项目分部截图:
二、前端 index.jsp 页面:
<body>
<!--注意这里form表单的填写-->
<form action="upload" enctype="multipart/form-data" method="post">
姓名:<input type="text" name="name"/><br/>
文件:<input type="file" name="file"/><br/>
<input type="submit" value="提交"/>
</form>
三、controller层代码:
@Controller
public class DemoController {
@RequestMapping(“upload”)
//这里的MultipartFile,取得文件名file一定要与前端form表单中的文件名name=“file"相//同
public String upload(MultipartFile file, String name,HttpServletRequest req) throws IOException {
// 获得服务器根目录
String serverPath=req.getServletContext().getRealPath(”/");
String fileName = file.getOriginalFilename();
System.out.println(“获得原始文件名:” + fileName);
String suffix = fileName.substring(fileName.lastIndexOf("."));
System.out.println(“获得文件扩展名:” + suffix);
// // 判断上传文件类型
// if (suffix.equalsIgnoreCase(".jpg")) {
String uuid = UUID.randomUUID().toString();
System.out.println(“获得一个随机ID:”+uuid);
// 进行文件的上传,简单说就是拷贝(拷贝到E盘路径下)
FileUtils.copyInputStreamToFile(file.getInputStream(), new File(
serverPath +"/files"+ uuid + suffix));
return “/uploadSuccess.jsp”;
// } else {
// return “/error.jsp”;
// }
}
@RequestMapping(“test”)
public void test01(){
System.out.println(“测试”);
}
}
三、web.xml 配置项:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>nueo_springMvc_upload</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<!-- 配置springmvc前端控制器(DispatcherServlet) -->
<servlet>
<servlet-name>mvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<!-- 设定配置文件的位置 -->
<param-value>classpath:applicationContext.xml</param-value>
</init-param>
<!-- 服务器启动后,自动加载 -->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>mvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- 字符编码过滤器 -->
<filter>
<filter-name>encoding</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>utf-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
</web-app>
四、applicationContext.xml配置文件:
<?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:p="http://www.springframework.org/schema/p"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">
<!-- 扫描注解 -->
<context:component-scan base-package="com.bjsxt.controller"></context:component-scan>
<!-- 注解驱动 -->
<!-- org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping -->
<!-- org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter -->
<mvc:annotation-driven></mvc:annotation-driven>
<!-- 静态资源 -->
<mvc:resources location="/js/" mapping="/js/**"></mvc:resources>
<mvc:resources location="/css/" mapping="/css/**"></mvc:resources>
<mvc:resources location="/images/" mapping="/images/**"></mvc:resources>
<mvc:resources location="/files/" mapping="/files/**"></mvc:resources>
<!-- MultipartResovler解析器 ,注意这里的名称不能随便改-->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- 指定上传文件的大小 -->
<property name="maxUploadSize" value="1000000"></property>
</bean>
<!-- 异常解析器 -->
<bean id="exceptionResolver" class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
<property name="exceptionMappings">
<props>
<prop key="org.springframework.web.multipart.MaxUploadSizeExceededException">/error.jsp</prop>
</props>
</property>
</bean>
</beans>