1.删除指定文件路径
1 public @ResponseBody 2 String deleteFiles(HttpServletRequest request) { 3 log.info(this.getClass().getSimpleName() + "." 4 + Thread.currentThread().getStackTrace()[1].getMethodName() 5 + "()------start"); 6 // 身份校验 7 String random = request.getParameter("random"); 8 String checkCode = request.getParameter("checkCode"); 9 String reCheckCode=MD5Util.encrypt("pplovemm"+random.substring(random.length()-6)); 10 String result = ""; 11 if(reCheckCode.equals(checkCode)){ 12 // 指定路径文件 13 String newFilePath="E:\video\2017-07-07\"; 14 log.info("Delete file or path:"+newFilePath); 15 System.out.println("Delete file or path:"+newFilePath); 16 if (newFilePath.startsWith(newFilePath) && clearFiles(newFilePath)) { 17 result = "success"; 18 } else { 19 result = "fail"; 20 } 21 log.info(this.getClass().getSimpleName() + "." 22 + Thread.currentThread().getStackTrace()[1].getMethodName() 23 + "()--------end"); 24 return result; 25 }else { 26 result = "fail"; 27 log.info(this.getClass().getSimpleName() + "." 28 + Thread.currentThread().getStackTrace()[1].getMethodName() 29 + "()-身份校验失败--end"); 30 return result; 31 }
2.clearFiles方法:
1 // 删除文件和目录 2 private static boolean clearFiles(String workspaceRootPath) { 3 File file = new File(workspaceRootPath); 4 if (file.exists()) { 5 deleteFile(file); 6 } 7 // resources 文件夹被删除后需新建 8 if (!file.exists() && workspaceRootPath.endsWith("resources")) { 9 return file.mkdir(); 10 }else if(!file.exists()){ 11 return true; 12 } 13 return false; 14 }