1文件上传
当enctype="multipart/form-data"时request.getParameter()方法失效
单文件上传
导入坐标
commons-fileupload commons-fileupload 1.3.1 commons-io commons-io 2.6
配置文件上传解析器
在spring-mvc.xml中加入
这里上传到webapp下
@RequestMapping(value = "/quick22")
@ResponseBody //不进行页面跳转不进行数据回写响应体为空
public void save22(String username, MultipartFile uploadFile1, MultipartFile uploadFile2) throws IOException {
System.out.println(username);
//
String originalFilename1 = uploadFile1.getOriginalFilename();
String originalFilename2 = uploadFile2.getOriginalFilename();
//获取servletContext对象
WebApplicationContext webApplicationContext = ContextLoader.getCurrentWebApplicationContext();
ServletContext servletContext =
//获取真实路径
webApplicationContext.getServletContext();
String realPath = servletContext.getRealPath("/");
//上传到哪儿
uploadFile1.transferTo(new File(realPath+originalFilename1));
uploadFile2.transferTo(new File(realPath+originalFilename2));
}
多文件上传
前台
客户端用数组接循环存储就行
@RequestMapping(value = "/quick23")
@ResponseBody //不进行页面跳转不进行数据回写响应体为空
public void save23(String username, MultipartFile[] uploadFile) throws IOException {
System.out.println(username);
WebApplicationContext webApplicationContext = ContextLoader.getCurrentWebApplicationContext();
ServletContext servletContext = webApplicationContext.getServletContext();
String realPath = servletContext.getRealPath("/");
for (MultipartFile multipartFile : uploadFile) {
String originalFilename = multipartFile.getOriginalFilename();
multipartFile.transferTo(new File(realPath+originalFilename
));
}
}
2,springMVC拦截器
类似Servlet开发中的过滤器Filter 用于处理器的预处理和后处理
不会拦截js,css html jsp
1)创建MyInterceptorl一个类实现implements HandlerInterceptor
有三个方法
//在目标方法值前
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
return flase;//放不放行
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
//再返回视图时执行
}
//在返回视图后执行
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
}
modelAndView.addObject("info","类转换异常");
}
modelAndView.setViewName("error");
return modelAndView;
}
}
在前端取数据${info}



