1. Upload files to a specified address in the project
@ResponseBody // return json data @ApiOperation (value = "image upload generates an address") @ RequestMapping (value = "/ testUploadimg", Method, = RequestMethod.POST) public String uploadImg (@RequestParam ( "File") MultipartFile File, the HttpServletRequest Request) { String path = ""; String contentType = file.getContentType (); String fileName = file.getOriginalFilename (); // automatically acquired project root String filePath = request.getSession () getServletContext ( ) getRealPath (.. "/") + "img / "; // this can fill a position corresponding // String filePath = ""; IF (file.isEmpty ()) { return "file is empty!"; } The try { // save the file to the project path = uploadFile (file.getBytes (),filePath, fileName); } the catch (Exception E) { // the TODO: Exception handle } // 返回json return path; } public String uploadFile(byte[] file, String filePath, String fileName) throws Exception { String path = ""; File targetFile = new File(filePath); if (!targetFile.exists()) { targetFile.mkdirs(); } FileOutputStream out = new FileOutputStream(filePath + "/" + fileName); path = filePath + "/" + fileName; out.write(file); out.flush(); out.close(); return path; }
2. Delete the specified files and folders in the project
Boolean filetest public (String path) { // delete the file String strVectorFile = path; Boolean BO = the deleteFile (strVectorFile); // delete folders String strVectorDir = "D: \\ test2"; the deleteDirectory (strVectorDir); return BO; } static Boolean the deleteFile public (String fileName) { File File = new new File (fileName); IF (file.isFile () && File.Exists ()) { Boolean succeedDelete File.delete = (); IF (succeedDelete) { the System.out .println ( "delete a single file" + fileName + "success!"); return to true; } {the else ( "! failed" "delete a single file" + fileName +) System.out.println; return to true; } } else { System.out.println ( "delete a single file" + fileName + "failed!"); Return to false; } } public static Boolean the deleteDirectory (String dir) { // If dir is not a file ending delimiter, the file is automatically added delimiter {IF (dir.endsWith (the File.separator)!) dir = dir + the File.separator; } file dirFile = new new file (dir); // if dir corresponding file does not exist, or is not a directory, exit if ( !! dirFile.exists () || dirFile.isDirectory ()) { System.out.println ( "delete directory failed" + dir + "directory does not exist!"); return to false; } Boolean = In Flag to true; // delete All files in the folder (including subdirectories) file [] = dirFile.listFiles files (); for (int I = 0; I <files.length; I ++) { // delete the subfile IF (Files [I] .isFile ()) { In Flag = the deleteFile (Files [I] .getAbsolutePath ()); IF (In Flag!) { BREAK; } } // delete the subdirectory the else { In Flag = the deleteDirectory (Files [I ] .getAbsolutePath ()); IF (Flag)! { BREAK; } } } IF (Flag) {! System.out.println ( "delete directory failed"); return false; } // remove the current directory if (dirFile. the delete ()) { System.out.println ( "remove directory" + dir + "success!"); return to true; } the else { System.out.println ( "remove directory" + dir + "failed!"); return to false; } }