1. form 表单的 enctype(enctype 是表单请求正文的类型)取值必须是:multipart/form-data(默认值 application/x-www-form-urlencoded)
2. method 属性取值必须是 Post
3. 提供一个文件选择域 < input type=”file” />
<dependency>
<groupId>commons-fileuploadgroupId>
<artifactId>commons-fileuploadartifactId>
<version>1.3.1version>
dependency>
<dependency>
<groupId>commons-iogroupId>
<artifactId>commons-ioartifactId>
<version>2.4version>
dependency>
<h3>文件上传h3>
<form action="user/fileupload" method="post" enctype="multipart/form-data">
选择文件:<input type="file" name="upload"/><br/>
<input type="submit" value="上传文件"/>
form>
@RequestMapping(value="/fileupload")
public String fileupload(HttpServletRequest request,MultipartFile upload) throws Exception {
System.out.println("SpringMVC方式的文件上传...");
// 先获取到要上传的文件目录
String path = request.getSession().getServletContext().getRealPath("/uploads");
// 创建 File 对象,向该路径下上传文件
File file = new File(path);
// 判断路径是否存在,不存在则创建该路径
if(!file.exists()) {
file.mkdirs();
}
// 获取到上传文件的名称
String filename = upload.getOriginalFilename();
// 把文件的名称唯一化
String uuid = UUID.randomUUID().toString().replaceAll("-", "").toUpperCase();
filename = uuid+"_"+filename;
// 上传文件
upload.transferTo(new File(file,filename));
return "success";
}
<!-- 配置文件解析器对象,要求 id 名称必须是 multipartResolver -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize" value="10485760"/>
</bean>
controller 调用 service、service 调用 dao,异常都是向上抛出的,最终由 DispatcherServlet 找异常处理器进行异常的处理
package com.mvc.exception;
public class SysException extends Exception {
//存储提示信息
private String message;
getter、setter 省略
}
package com.mvc.exception;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class SysExceptionResolver implements HandlerExceptionResolver {
@Override
public ModelAndView resolveException(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) {
//获取到异常对象
SysException sysException = null;
if (e instanceof SysException){
sysException = (SysException) e;
}else {
sysException = new SysException("系统正在维护...");
}
//创建 ModelAndView 对象
ModelAndView mv = new ModelAndView();
// 存入错误的提示信息
mv.addObject("errorMsg",sysException.getMessage());
// 跳转的 Jsp 页面
mv.setViewName("error");
return mv;
}
}
<bean id="sysExceptionResolver" class="com.mvc.exception.SysExceptionResolver">bean>
package com.mvc.interceptor;
import org.springframework.web.servlet.HandlerInterceptor;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class MyIntercepter1 implements HandlerInterceptor {
/**
* 预处理,controller 方法执行前
* return true:放行,执行下一个拦截器。如果没有,执行 conroller 方法
* return false:不放行
*/
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
System.out.println("Interceptor运行成功");
return true;
}
}
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/user/*"/>
<bean class="com.mvc.interceptor.MyIntercepter1">bean>
mvc:interceptor>
mvc:interceptors>