/**
* 从本地目录中清除文件包括文件夹
* @param path
*/
public static void deleteAllFiles(File path) {
if (!path.exists())
return;
if (path.isFile()) {
path.delete();
return;
}
File[] files = path.listFiles();
for (int i = 0; i < files.length; i++) {
deleteAllFiles(files[i]);
}
path.delete();
}
调用:
//D:\workspace\AA\uploads\advertise\f9e2d7ffdcfa471ab9dc0a846de8ec23
String adFilePath = Constant.AD_FILE_UPLOAD_FOLDER + File.separator
+ fileFolder + File.separator + adPackageId ;
File file = new File(ServletActionContext.getServletContext()
.getRealPath(File.separator) + adFilePath);
//如果文件夹为空,直接删除文件夹,否则先清空文件夹中文件再删除文件夹
StringUtil.deleteAllFiles(file);