我可以使用Struts2上传文件,但我想删除指定的目录,但我不知道如何删除。
这是我的代码:
public String execute(){
destPath = "/tmp/listfile";
try{
System.out.println("Src File name: " + myFile);
System.out.println("Dst File name: " + myFileFileName);
File destFile = new File(destPath, myFileFileName);
FileUtils.copyFile(myFile, destFile);
fileList = ListFiles.ListAllFiles("/tmp/listfile");
return "listfiles";
}
catch(IOException e)
{
e.printStackTrace();
return "ERROR";
}
}
如果您使用Apache Commons IO,它就是一个单行:
FileUtils.deleteDirectory(dir);
我想你想移动文件而不是复制
public String execute() {
String destPath = "/tmp/listfile";
try {
System.out.println("Src File name: " + myFile);
System.out.println("Dst File name: " + myFileFileName);
Path source = Paths.get(myFile.getAbsolutePath());
Path target = Paths.get(destPath);
Files.move(source, target.resolve(myFileFileName), REPLACE_EXISTING);
fileList =ListFiles.ListAllFiles("/tmp/listfile");
return "listfiles";
} catch(IOException e) {
e.printStackTrace();
return "ERROR";
}
}
FileUtils.getFile(destFile).delete();