我可以使用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";
}
}
答案 0 :(得分:1)
如果你使用Apache Commons IO,它就是一个单行:
FileUtils.deleteDirectory(dir);
答案 1 :(得分:1)
我认为你想移动文件而不是复制
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";
}
}
答案 2 :(得分:0)
FileUtils.getFile(destFile).delete();