标签:style class blog code java http
用到这两个包
配置视图解析器:springmvc配置文件配置
<!-- id必须要是“multipartResolver” --> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <!-- maxUploadSize文件上传的最大值,单位是byte --> <property name="maxUploadSize" value="1024000000"></property> </bean>Form.jsp
<form action="test/toPerson8.do" method="post" enctype="multipart/form-data"> name:<input name="name" type="text"><br/> age:<input name="age" type="text"><br/> address:<input name="address" type="text"><br/> birthday:<input name="birthday" type="text"><br/> pic:<input type="file" name="pic"><br/> <input type="submit"><br/> </form>
@RequestMapping(value="/toPerson8.do") public String toPerson8(Person person,HttpServletRequest request) throws Exception{ //第一步转化request MultipartHttpServletRequest rm = (MultipartHttpServletRequest) request; //获得文件 CommonsMultipartFile cfile = (CommonsMultipartFile) rm.getFile("pic"); //获得文件的字节数组 byte[] bfile = cfile.getBytes(); String fileName = ""; //获得当前时间的最小精度 SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmssSSS"); fileName = format.format(new Date()); //获得三位随机数 Random random = new Random(); for(int i = 0; i < 3; i++){ fileName = fileName + random.nextInt(9); } //获得原始文件名 String origFileName = cfile.getOriginalFilename(); //XXX.jpg String suffix = origFileName.substring(origFileName.lastIndexOf(".")); //拿到项目的部署路径 String path = request.getSession().getServletContext().getRealPath("/"); //定义文件的输出流 OutputStream out = new FileOutputStream(new File(path+"/upload/"+fileName+suffix)); out.write(bfile); out.flush(); out.close(); return "jsp/index"; }重启tomcat
三个方法分别对应前面图三个拦截,
Perihandle前置拦截:最多的是对权限的拦截
/** * 执行时机:在执行controller之前来执行 * 返回值类型:boolean:true代表放行可以访问controller, * false表示不可以访问controller */ public boolean preHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2) throws Exception { return false; } }Posthandle后置拦截:
/** * 执行时机:controller执行完,视图解析器没有把视图解析成页面 * 对视图做统一的修改,主要提现的Model上 */ public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, ModelAndView mv) throws Exception { }
最终拦截:
/** * 执行时机:视图已经被解析完毕,类似try catch 后的finally */ public void afterCompletion(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, Exception arg3) throws Exception { }在每个方法输出一句话:
System.out.println("afterCompletion...");
System.out.println("postHandle...");
System.out.println("preHandle...");
将前置拦截改成true;<mvc:interceptors> <mvc:interceptor> <!-- 对所有的请求都拦截,使用/**,对某个模块下的请求做拦截/模块名/* --> <mvc:mapping path="/**"/> <bean class="cn.edu.hqu.interceptor.MyInterceptor"></bean> </mvc:interceptor> </mvc:interceptors>
将前置拦截改成false
访问
http://localhost:8080/springmvc-2/test/toForm.do
无法进行跳转;控制台输出:
preHandle...
前置拦截 改成true
输出拦截器的test
后置拦截:
public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, ModelAndView mv) throws Exception { System.out.println("postHandle..."); Map<String, Object> map = mv.getModel(); map.put("test", "addpend something by Jerome"); }
重启tomcat 访问
http://localhost:8080/springmvc-2/test/toPerson5.do
输出:
…
addpend something by Jerome
模拟异常:
public void afterCompletion(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, Exception ex) throws Exception { System.out.println("afterCompletion..."); ex.printStackTrace(); System.out.println("==========="); }在页面输出 :jsp/index.jsp下面加 <%=1/0 %>
拦截器里面方法的输出;这捕捉到异常可以做日志监控方面的工作;
补充:
加载配置文件,我们配置文件一般放在config下,名字也可以自己取;
只要在web.xml配置
<!-- springmvc的中央控制器 --> <servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:springmvc.xml</param-value> </init-param> </servlet>初始化配置文件;
源码下载:http://pan.baidu.com/s/1mg3GcLq
学习笔记_springmvc文件上传和拦截器,布布扣,bubuko.com
标签:style class blog code java http
原文地址:http://blog.csdn.net/jerome_s/article/details/28407313