繁体   English   中英

使用Struts 2上传后如何删除文件?

[英]How to delete a file after uploading using Struts 2?

我可以使用 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();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM