使用java复制文件会在tomcat安装目录下生成不需要的副本

时间:2015-02-23 11:57:02

标签: java tomcat tomcat7

在我的网络应用程序中,我使用java将文件从一台计算机复制到另一台计算机。它工作正常,但我获得了tomcat安装目录下每个文件的额外副本。似乎java使用tomcat目录作为临时文件夹将文件从一个地方移动到另一个地方,并且从不清理它的混乱。 我该如何避免这种行为?

这是我的复制功能

private static void copyFile(File sourceFile, File targetDir) throws IOException, FileAlreadyExistsException {

        File destFile = new File(targetDir, sourceFile.getName());

        if(destFile.exists()) {
            throw new FileAlreadyExistsException(destFile.getAbsolutePath());
        } else {
            if(!targetDir.exists()){
                targetDir.mkdirs();
            }
            destFile.createNewFile();
        }

        FileChannel source = null;
        FileChannel destination = null;

        try {
            source = new FileInputStream(sourceFile).getChannel();
            destination = new FileOutputStream(destFile).getChannel();
            destination.transferFrom(source, 0, source.size());
        }
        finally {
            if(source != null) {
                source.close();
            }
            if(destination != null) {
                destination.close();
            }
        }
    }

我正在使用renameTo移动文件。

0 个答案:

没有答案