写入后无法删除文件

时间:2013-07-31 19:51:27

标签: java file inputstream delete-file

我会把我的代码放在第一位:

@Post
public Representation post(InputStream zip) throws Throwable {
    createFile(zip, "C:/temp");
    return new StringRepresentation("File uploaded");
}    

public void createFilee(InputStream zipStream, uploadedFileLocation) throws Exception {
    try {
        writeToFile(zipStream, uploadedFileLocation);
        FileUtils.forceDelete(new File(uploadedFileLocation));
        } catch (Exception e) {
             throw e;
        }
}


private void writeToFile(InputStream uploadedInputStream, String uploadedFileLocation) {
    try {
        OutputStream out = new FileOutputStream(new File(uploadedFileLocation));
        int read = 0;
        byte[] bytes = new byte[1024];

        out = new FileOutputStream(new File(uploadedFileLocation));
        while ((read = uploadedInputStream.read(bytes)) != -1) {
            out.write(bytes, 0, read);
        }
        out.flush();
        out.close();
        uploadedInputStream.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

当我将文件发送到我的服务器时,它无法删除。使用FileUtils.forceDelete()时,它表示无法删除文件。在尝试使用文件utils删除文件后,我可以在服务器仍在运行时手动删除该文件。为什么不能自己删除它?!有任何想法吗?感谢

编辑:问题可能是POST的InputStream还活着,直到POST返回?所以,即使我打电话删除文件,POST仍然保持活着的流?这甚至可能吗?

1 个答案:

答案 0 :(得分:1)

在我有限的Windows体验中,它可能是两件事之一

我会检查 1)防病毒软件正在尝试扫描该文件 2)某种索引器(系统或自定义)正在尝试索引文件。

您可以使用processExplorer之类的工具来查看阻止文件描述符的进程。 http://technet.microsoft.com/en-us/sysinternals/bb896653.aspx

相关问题