1. File Upload
1.1 preparatory work, we need two jar package ( the Fileupload )
jar package download path:
[Download https://github.com/suyirulan/putao/tree/master/fileupload_jar ]
Jsp page upload 1.2 below, note sheet form tag attribute file uploading method = "post" enctype = " multipart / form-data" is required , and the file name = "" is also necessary
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <!DOCTYPE html> 4 <html> 5 <head> 6 <meta charset="UTF-8"> 7 <title>上传文件</title> 8 </head> <9 body> 10 <form action="addStudent" method="post" enctype="multipart/form-data"> 11 姓名:<input type="text" name="name"/><br> 12 年龄:<input type="text" name="age"/><br> 13 头像:<input type="file" name="headphoto"/><br> 14 <input type="submit" value="上传"/> 15 </form> 16 </body> 17 </html>
1.3 Upload configuration file parser springmvc
Wherein value = "99999999" to limit the maximum number of bytes uploaded file (bytes: 1M = 1024K = 1024 * 1024 = 1048576 bytes )
1 <!-- 文件上传解析器 --> 2 <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> 3 <property name="maxUploadSize" value="99999999"></property> 4 </bean>
Controller Code 1.4 Upload file
. 1 @Controller 2 public class StudentController { . 3 . 4 @RequestMapping ( "addStudent" ) . 5 public String addStudent (Student STU, the HttpServletRequest Request, a MultipartFile headphoto, the Model Model) { . 6 // 1. Get real path to save a file to upload . 7 String path = . request.getServletContext () getRealPath ( "/ headphoto" ); 8 // 2. create a file object 9 file file = new new file (path); 10 // If the file does not create 11 IF (! File.Exists ( )) { 12 file.mkdirs(); 13 } 14 //3.获取文件名 15 String filename=System.currentTimeMillis()+headphoto.getOriginalFilename(); 16 17 stu.setPhoto("headphoto/"+filename); 18 19 File targetFile =new File(path+"/"+filename); 20 21 try { 22 FileUtils.writeByteArrayToFile(targetFile, headphoto.getBytes()); 23 } catch (IOException e) { 24 // TODO Auto-generated catch block 25 e.printStackTrace(); 26 } 27 28 model.addAttribute("stu",stu); 29 30 return "index"; 31 } 32 }
carry out testing
Upload successful
2. interceptor (control layer address are intercepted, filter: page address can intercept requests)
2.1 create a class that implements the interface Handlerinterceptor
2.2 Method rewriting interface
1 public class MyInterceptor implements HandlerInterceptor{ 2 3 @Override 4 public void afterCompletion(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, Exception arg3) 5 throws Exception { 6 // TODO Auto-generated method stub 7 8 } 9 10 @Override 11 public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, ModelAndView arg3) 12 throws Exception { 13 // TODO Auto-generated method stub 14 15 } 16 17 @Override 18 public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object arg2) throws Exception { 19 Object name = request.getSession().getAttribute("name"); 20 if(name!=null) { 21 return true; 22 }else { 23 System.out.println("拦截器"); 24 return false; 25 } 26 } 27 28 }
2.3把创建的类配置到SpringMVC文件中,其中<mvc:mapping path=" "/>为拦截的,<mvc:exclude-mapping path=" "/>为放行的
1 <!-- 拦截器 --> 2 <mvc:interceptors> 3 <mvc:interceptor> 4 <mvc:mapping path="/user/**"/> 5 <mvc:exclude-mapping path="/user/login1"/> 6 <mvc:exclude-mapping path="/user/denglu"/> 7 <bean class="com.zhiyou100.zhl.interceptor.MyInterceptor"></bean> 8 </mvc:interceptor> 9 </mvc:interceptors>
Fileupload