0

我可以使用 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";
  }
}
4

3 回答 3

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";
   }
}
于 2013-07-11T09:10:27.123 回答
1

如果您使用 Apache Commons IO,它是单行的:

FileUtils.deleteDirectory(dir);
于 2013-07-11T06:42:40.793 回答
0
FileUtils.getFile(destFile).delete();
于 2019-03-25T15:56:20.253 回答