springboot upload files to the server


@RequestMapping ( value = "/upload" )
 @ResponseBody
 // Upload a file to the server
 public HttpResponseEntity upload ( @RequestParam ( "file" ) MultipartFile file) {
 service .saveFile(file)) ;
 } 
/******* ******Useless codes are deleted ***********************              

  
 
 
saveFile(MultipartFile file) {
             // Get the file name
             String fileName = file.getOriginalFilename() ;
             System. out .println( "The uploaded file name: " + fileName) ;
 // Get the file suffix name
 String suffixName = fileName. substring(fileName.lastIndexOf( "." )) ;
 System. out .println( " The suffix of the file is: " + suffixName) ;
 // Set the file storage path
 String filePath = "E:/file/" ;
 System. out .println( "The file storage path is: " + filePath)                                    
                                    ;
 // Name the new file
 with uuid String fileUUName = UUID. randomUUID ().toString() ;
 String path = filePath + fileUUName + suffixName ;
 System. out .println(path) ;
 // Create a new file
 File dest = new File(path) ;
 // Check if a directory exists
 if (!dest.getParentFile().exists()) { 
                dest.getParentFile().mkdirs() ; // Create a new folder
 } 
            file.transferTo(dest) ; // the file file is written to dest
 //                                                                                                                        The above process is the function of uploading pictures
 }
          


    



Guess you like

Origin blog.csdn.net/qq_37790902/article/details/79583818