SpringMVC ------- 3. File upload, interceptors and data validation (background check)

1. File Upload

1.1 Import jar package

 

 1.2 Setting Properties form submission

File upload is only allowed to post form submission, and the encoding type multipart / form-data

 

 1.3 In springmvc configuration file upload parser.

Where id name can not be changed, otherwise an error

Set the maximum upload size maxUploadSize

 

 

 1.4 in the control layer processing code

 

@ RequestMapping ( "the Upload" )
     public String the Upload (MultipartFile myfile, the HttpServletRequest Request) {
         // 1. Get file upload real save path 
        String path = request.getServletContext () getRealPath ( "/ the Upload." ); 
        
        // 2. Creating a file object 
        file file = new new file (path);
         IF (! {File.Exists ()) 
            file.mkdirs (); 
        } 
        // 3. get file name 
        String name = System.currentTimeMillis () + myfile.getOriginalFilename () ; 
        file targetfile = new new file (path + "/" + name); 
        
        // write 4. file to the specified directory to 
        try {
            FileUtils.writeByteArrayToFile(targetFile, myfile.getBytes());
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return "index";
    }

 2. Use of interceptors ( intercept all the control layer address. Filter:)

2.1  to create a class that implements the interface HandlerInterceptor, rewrite inside method

public class MyInterceptor implements HandlerInterceptor{

    @Override
    public void afterCompletion(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, Exception arg3)
            throws Exception {
        
    }

    @Override
    public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, ModelAndView arg3)
            throws Exception {
    }

    @Override
    public boolean preHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2) throws Exception {
        return false;
        
    }

2.2  your creation to class configuration springmvc file

 

 

 3. The data check (background check)

3.1 introduces jar package

 

 

 3.2 annotations corresponding entity class

 

Notes categories:

 

 

 

3.3 accept parameters in the control layer

 

Guess you like

Origin www.cnblogs.com/zyl187110/p/11462175.html