无法删除临时文件夹(有时)

发布于 2024-11-19 12:28:15 字数 1294 浏览 2 评论 0原文

当我启动应用程序时,我创建一个临时文件夹:

  public static File createTempDir(String name) throws IOException {
    File tempDir = File.createTempFile(name, "");
    if (!(tempDir.delete())) {
      throw new IOException("could not delete" + tempDir.getAbsolutePath());
    }

    if (!(tempDir.mkdir())) {
      throw new IOException("could not create" + tempDir.getAbsolutePath());
    }
    tempDir.deleteOnExit();
    return tempDir;
  }

在会话期间,用户可能会加载文件。因此,旧的临时目录将被删除,并根据加载的文件的 ID 创建新的临时目录。

在删除旧临时目录的加载过程中,我有时会得到一个:

java.io.IOException:无法删除文件:

以下是删除旧临时文件夹的方式:

  public void cleanup(String tmpPath) {
    File tmpFolder = new File(tmpPath);
    if (tmpFolder != null && tmpFolder.isDirectory()) {
      try {
        FileUtils.deleteDirectory(file);
      } catch (IOException e) {
            e.printStackTrace();
      }
    } 
  }

其中FileUtils是:org.apache.commons.io.FileUtils。通常临时文件夹的内容是:

mytempfolder_uuid |->我的子文件夹 |-> myImage.jpg

错误是:

java.io.IOException: 无法删除文件: C:\Users\xxx\AppData\Local\Temp\mytempfolder_uuid\mysubfolder\myImage.jpg

我曾尝试调试应用程序并在删除之前执行操作验证上述图像实际上位于指定文件夹中。

令人讨厌的是,这种情况只是偶尔发生。我已确保不要在任何其他应用程序中打开临时文件夹中的文件夹/文件。有什么想法/建议吗?

When I start my application I create a temp folder:

  public static File createTempDir(String name) throws IOException {
    File tempDir = File.createTempFile(name, "");
    if (!(tempDir.delete())) {
      throw new IOException("could not delete" + tempDir.getAbsolutePath());
    }

    if (!(tempDir.mkdir())) {
      throw new IOException("could not create" + tempDir.getAbsolutePath());
    }
    tempDir.deleteOnExit();
    return tempDir;
  }

During a session a user might load a file. As a result the old temp dir is deleted and a new is created based on the ID of the file loaded.

During load where the old temp dir is deleted I sometimes get a:

java.io.IOException: Unable to delete file:

Here is how the old temp folder is deleted:

  public void cleanup(String tmpPath) {
    File tmpFolder = new File(tmpPath);
    if (tmpFolder != null && tmpFolder.isDirectory()) {
      try {
        FileUtils.deleteDirectory(file);
      } catch (IOException e) {
            e.printStackTrace();
      }
    } 
  }

where FileUtils is: org.apache.commons.io.FileUtils. Typically the content of the temp folder is:

mytempfolder_uuid
|-> mysubfolder
|-> myImage.jpg

And the error is:

java.io.IOException: Unable to delete file: C:\Users\xxx\AppData\Local\Temp\mytempfolder_uuid\mysubfolder\myImage.jpg

I have tried to debug the application and before the delete operation is executed verified that the above image is actually located in the specified folder.

The nasty thing is that it only happens sometimes. I have made sure not to have the folder/files in the temp folder open in any other applications. Any ideas/suggestions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

又爬满兰若 2024-11-26 12:28:15

您无法删除打开的文件,也无法删除包含文件的目录。您必须确保目录中的所有文件都已关闭。

You cannot delete files which are open and you can't delete a directory which contains a file. You have to ensure all files in the directory are closed.

安人多梦 2024-11-26 12:28:15

我建议你使用 Guava 库。它有一个方法 Files.createTempDir() 完全符合您的需要:

在系统目录下的某处自动创建一个新目录
临时目录(由 java.io.tmpdir 系统定义
属性),并返回其名称。使用此方法代替
File.createTempFile(String, String) 当你想创建一个
目录,而不是常规文件。 一个常见的陷阱是调用
createTempFile,删除该文件并在其位置创建一个目录,
但这会导致竞争条件,可以利用它来创建
安全漏洞,尤其是当可执行文件被
写入目录。
此方法假设临时
卷是可写的,具有空闲 inode 和空闲块,并且它将
每秒不会被调用数千次。

I'd suggest you use the Guava library. It has a method Files.createTempDir() that does exactly what you seem to need:

Atomically creates a new directory somewhere beneath the system's
temporary directory (as defined by the java.io.tmpdir system
property), and returns its name. Use this method instead of
File.createTempFile(String, String) when you wish to create a
directory, not a regular file. A common pitfall is to call
createTempFile, delete the file and create a directory in its place,
but this leads a race condition which can be exploited to create
security vulnerabilities, especially when executable files are to be
written into the directory.
This method assumes that the temporary
volume is writable, has free inodes and free blocks, and that it will
not be called thousands of times per second.

三寸金莲 2024-11-26 12:28:15

在删除之前尝试删除临时文件夹中的文件。尝试

private boolean deleteFolder(File path) {
    if (path.exists()) {
        File[] files = path.listFiles();
        for (File f : files) {
            if (f.isDirectory()) {
                deleteFolder(f);
            } else {
                f.delete();
            }
        }
    }

    return path.delete();
}

使用deleteOnExit之类的东西并不是一个好主意......

干杯!

try deleting the files in the temp folder before deleting it. Try somethng like

private boolean deleteFolder(File path) {
    if (path.exists()) {
        File[] files = path.listFiles();
        for (File f : files) {
            if (f.isDirectory()) {
                deleteFolder(f);
            } else {
                f.delete();
            }
        }
    }

    return path.delete();
}

also using deleteOnExit is not a very good idea...

cheers!

酒儿 2024-11-26 12:28:15
public static boolean deleteDir(String path)
{
    java.io.File dir = new java.io.File(path);
    if (dir.isDirectory())
    {
        String[] filesList = dir.list();
        for(String s : filesList)
        {
            boolean success = new java.io.File(dir, s).delete();
            if(!success)
            {
                return false;
            }
        }
    }
    return dir.delete();
}

然后你可以像这样使用它: deleteDir("C:\\MyFolder\\subFolder\\")

public static boolean deleteDir(String path)
{
    java.io.File dir = new java.io.File(path);
    if (dir.isDirectory())
    {
        String[] filesList = dir.list();
        for(String s : filesList)
        {
            boolean success = new java.io.File(dir, s).delete();
            if(!success)
            {
                return false;
            }
        }
    }
    return dir.delete();
}

and then you can use it like: deleteDir("C:\\MyFolder\\subFolder\\")

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文