我正在使用FileUtils.saveAndUnzipToTempDir
将文件夹/文件解压缩到临时目录,并且一旦完成浏览文件夹,便想删除该文件夹。
我没有很好的Java经验,但是我认为saveAndUnzipToTempDir
进程尚未完成,并且不知道如何等待该进程完成或如何终止它:>
这是我的代码:
public boolean postFile(@RequestParam String filename, @RequestParam MultipartFile file) {
FileResultsDTO fileResultsDTO = FileUtils.saveAndUnzipToTempDir(file, filename);
for(String fileName : fileResultsDTO.getUnzipDir().list()){
// do something.
}
//Find the temp folder where the other files/folders are uploaded and
//deleted those
File directory = new File(FileUtils.getTempDirectoryPath());
FileUtils.deleteDirectory(directory);
FileUtils.forceDelete(directory);
delete(directory);
}
public static void delete(File file)
throws IOException {
if(file.isDirectory()){
//directory is empty, then delete it
if(file.list().length==0){
FileDeleteStrategy.FORCE.delete(file);
FileUtils.deleteQuietly(file);
file.delete();
System.out.println("Directory is deleted : "
+ file.getAbsolutePath());
}else{
//list all the directory contents
String files[] = file.list();
for (String temp : files) {
//construct the file structure
File fileDelete = new File(file, temp);
//recursive delete
delete(fileDelete);
}
//check the directory again, if empty then delete it
if(file.list().length==0){
FileDeleteStrategy.FORCE.delete(file);
FileUtils.deleteQuietly(file);
file.delete();
System.out.println("Directory is deleted : "
+ file.getAbsolutePath());
}
}
}else{
//if file, then delete it
FileDeleteStrategy.FORCE.delete(file);
FileUtils.deleteQuietly(file);
file.delete();
System.out.println("File is deleted : " + file.getAbsolutePath());
}
}
因此,如果您仔细查看代码,则可以看到我也尝试过使用:
FileDeleteStrategy.FORCE.delete(file);
FileUtils.deleteQuietly(file);
file.delete();