文件上传
用到这两个包
配置视图解析器: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>
新建目录 upload在webroot下;存放上传的文件;
提交表单:
@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
http://localhost:8080/springmvc-2/test1/toForm.do
选择一个文件 提交,成功。
多了一个图片
在tomcat下:
D:javaapache-tomcat-6.0.37webappsspringmvc-2upload
拦截器:
新建包:cn.edu.hqu.interceptor类:MyInterceptor implements HandlerInterceptor;
三个方法分别相应前面图三个拦截,
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;配置拦截器:在springmvc配置文件配置
<mvc:interceptors> <mvc:interceptor> <!-- 对全部的请求都拦截,使用/**。对某个模块下的请求做拦截/模块名/* --> <mvc:mapping path="/**"/> <bean class="cn.edu.hqu.interceptor.MyInterceptor"></bean> </mvc:interceptor> </mvc:interceptors>
重新启动tomcat 訪问
http://localhost:8080/springmvc-2/test/toForm.do
控制台输出:
preHandle...
postHandle...
afterCompletion...
将前置拦截改成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"); }
Jsp/index.jsp
加 <h5>${test}</h5>
重新启动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 %>
訪问:http://localhost:8080/springmvc-2/test/toPerson5.do
异常:控制台
拦截器里面方法的输出;这捕捉到异常能够做日志监控方面的工作;
补充:
载入配置文件,我们配置文件一般放在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