MultipartFile upload pictures of writing, recording it.

Upload pictures of tools:

Code:

 1 import org.springframework.stereotype.Component;
 2 import org.springframework.web.multipart.MultipartFile;
 3 
 4 import javax.servlet.http.HttpServletRequest;
 5 import javax.servlet.http.HttpSession;
 6 import java.io.File;
 7 import java.io.IOException;
 8 import java.text.SimpleDateFormat;
 9 import java.util.Date;
10 import java.util.UUID;
11 
12 @Component
13 public class HttpRequestUploadServiceImpl the implements HttpRequestUploadService {
 14  
15      public  void uploadImage (a MultipartFile File, the HttpSession the session, the HttpServletRequest Request) throws IOException {
 16          System.out.println ( "Start Photo" );
 . 17          Request.setCharacterEncoding ( "UTF-. 8" );
 18 is          IF ( null ! = File) {
 19              // obtain the path of the current project is located 
20              String pathRoot = request.getSession () getServletContext () getRealPath ( ".". );
 21              System.out.println ( "where the path of the current project:" + pathRoot );
 22              //Uuid generated as a file name 
23 is              String uuid = UUID.randomUUID () toString () the replaceAll ( "-", ".". );
 24              System.out.println ( "File Name:" + uuid);
 25              // get files type (if not determine the type of image file is prohibited uploading) 
26              String contentType = file.getContentType ();
 27              System.out.println ( "file type:" + contentType);
 28              // get the file name suffix 
29              String imageName = contentType.substring (contentType.indexOf ( "/") +. 1 );
 30              System.out.println ( "file extension name:" + imageName);
31 
32             String filePath = "F:\\";
 33              // file to create corresponding to the date folder 
34 is              String = datePath new new the SimpleDateFormat ( "YYYY / the MM / dd /") the format (. New new a Date ());
 35              System.out.println ( "Date:" + datePath );
 36              // file to create corresponding sorted by date folder 
37 [              String path filePath = + datePath;
 38 is              // if not, create a new folder 
39              file F = new new file (path);
 40              IF (! F. EXISTS ()) {
 41 is                  f.mkdirs ();
 42 is              }
 43 is              // newly generated file name
44              . "" String fileName = uuid + + imageName;
 45              System.out.println ( "newly generated file name:" + fileName);
 46              session.setAttribute ( "fileName" , fileName);
 47              // save the picture complete path 
48              String pathName path + = fileName;
 49              System.out.println (pathName);
 50              // static resources image path 
51 is              String staticPath = "/ Upload / images /" + fileName;
 52 is              System.out.println ( "static resource path: "+ staticPath);
 53 is              // copy operation
 54              //The image copied from the source location to the target location 
55              file.transferTo ( new new File (pathName));
 56 is          }
 57 is      }
 58 }

 

Guess you like

Origin www.cnblogs.com/wangquanyi/p/11328879.html