Java将文件移动到另一个目录问题

时间:2017-09-05 07:27:29

标签: java

您好我已经制作了一个程序,我解组XML文件,获取这些信息并使用它们连接到服务器并将文件移动到另一个文件夹。 该计划如下: 1.get从xml获取信息 2.连接并将它们上传到服务器 3.将本地上传到另一个文件夹的文件移动。

问题是我把上传的对象和方法。上传没问题,上传文件,为文件移动创建新文件夹,但文件不移动,它们在同一目录中。

如果我把Move对象和方法放在上面,那就相反了。文件被移动到另一个文件夹(本地),但不会上传...

可能是什么问题以及如何解决这个问题? 谢谢!

这是我的沼泽方法,我使用这两种方法(上传和移动):

public void unmarshallList() {

    try {
        JAXBContext jc = JAXBContext.newInstance(ListNodes.class);
        Unmarshaller ums = jc.createUnmarshaller();
        ListNodes ln = (ListNodes) ums.unmarshal(new File("C:\\Users\\Desktop\\marshList.xml"));
        System.out.println("INFORMATIONS");
        System.out.println("-------------------------------");
        for (Node p : ln.getListNode()) {
            System.out.println("Hostname: " + p.getHostname());
            System.out.println("Username: " + p.getUsername());
            System.out.println("Password: " + p.getPassword());
            System.out.println("Port: " + p.getPort());
            System.out.println("Pc Directory: " + p.getPcDirectory());
            System.out.println("Node Directory: " + p.getNodeDirectory());
            System.out.println("Time interval: " + p.getTimeInterval());
            System.out.println("Move Directory" + p.getMoveDir());
            System.out.println("-------------------------------");



            Upload up = new Upload();
            up.connection(p.getHostname(), p.getPort(), p.getUsername(), p.getPassword(), p.getNodeDirectory(), p.getPcDirectory(), p.getTimeInterval(), p.getMoveDir());

            Move mv = new Move();
            mv.moveFiles(p.getPcDirectory(), p.getMoveDir());


        }

    } catch (Exception e) {
        // TODO: handle exception
        System.out.println(e.getMessage());
    }
}

这是我的移动方法:

    public static void moveFiles (String oldLocation, String newLocation) {
    File source = new File(oldLocation);
    File dest = new File(newLocation);

    new File(newLocation).mkdir();

    File[] files = source.listFiles();

    for (File file : source.listFiles()) {

        System.out.println(source + "\\" + file.getName());

        String x = (source + "\\" + file.getName());
        String y = (dest + "\\" + file.getName());


        File f1 = new File(x);
        f1.renameTo(new File(y));
        System.out.println("This file is moved "+x );
    }

    System.out.println("The files are moved" );
}

这是上传方法:

 private static void recursiveFolderUpload(String sourcePath, String 
destinationPath) throws SftpException, FileNotFoundException {

    File sourceFile = new File(sourcePath);
    if (sourceFile.isFile()) {

        // copy if it is a file
        channelSftp.cd(destinationPath);
        if (!sourceFile.getName().endsWith(".xml"));
            channelSftp.put(new FileInputStream(sourceFile), sourceFile.getName(), ChannelSftp.OVERWRITE);

    } else {

        System.out.println("inside " + sourceFile.getName());
        File[] files = sourceFile.listFiles();

        if (files != null && !sourceFile.getName().endsWith(".xml")) {

            channelSftp.cd(destinationPath);
            SftpATTRS attrs = null;

            // check if the directory is already existing
            try {
                attrs = channelSftp.stat(destinationPath + "/" + sourceFile.getName());
            } catch (Exception e) {
                System.out.println(destinationPath + "/" + sourceFile.getName() + " not found");
            }

            // else create a directory
            if (attrs != null) {
                System.out.println("Directory exists IsDir=" + attrs.isDir());
            } else {
                System.out.println("Creating dir " + sourceFile.getName());
                channelSftp.mkdir(sourceFile.getName());
            }

            for (File f: files) {
                recursiveFolderUpload(f.getAbsolutePath(), destinationPath + "/" + sourceFile.getName());
            }

        }
    }

1 个答案:

答案 0 :(得分:0)

我的猜测是源文件的InputStream没有关闭。

我试试

InputStream in = new FileInputStream(sourceFile)
try {
   channelSftp.put(in, sourceFile.getName(), ChannelSftp.OVERWRITE);
} finally {
   in.close();
}

或其他方式

try (InputStream in = new FileInputStream(sourceFile)) {
   channelSftp.put(in, sourceFile.getName(), ChannelSftp.OVERWRITE);
}

关闭退出try语句的InputStream uppon。

然而 - 没有调试/任何异常消息,几乎不可能确定