SFTP非空文件夹递归删除

时间:2014-12-01 16:27:05

标签: java sftp

我尝试通过SFTP删除非空文件夹的JSCH,但它不起作用。

这个有效,但它不是免费的:(我已经过了30天的试用期。我的公司拒绝支付他们只是通过SFTP删除文件夹。

package com.test.cvsutil;

import java.util.List;

import org.apache.log4j.Logger;

import com.jscape.inet.sftp.Sftp;
import com.jscape.inet.sftp.SftpException;
import com.jscape.inet.sftp.events.SftpAdapter;
import com.jscape.inet.ssh.util.SshParameters;

public class SFTPFileDeleter extends SftpAdapter {
     static String hostName = ServerProperties.getProperty("test.ftp.hostname");
     static String username = ServerProperties.getProperty("test.ftp.login");
     static String password = ServerProperties.getProperty("test.ftp.password");
     static String directory = ServerProperties.getProperty("test.ftp.directory");
    private static Sftp sftp;

    private static org.apache.log4j.Logger log = Logger.getLogger(SFTPFileDeleter.class);


    public static boolean deleteDir(List <String> path) throws SftpException {
        Boolean flag = false;
        log.info("------------------------ file(s) delete started ------------------------");
        sftp = new Sftp(new SshParameters(hostName, username, password));

        sftp.connect();
        sftp.setDir(directory);

        for (String eachOne : path) {
            if (!sftp.getDirListingAsString(eachOne).equals("")){
                log.info(" ------  Deleted Folder/Scenario Name: " + eachOne);
                //log.info(" ------  check file path: " + directory+eachOne);
                //System.out.println(directory+eachOne);
                sftp.deleteDir(directory+eachOne, true);
                flag = true;
            }
        }

        sftp.disconnect();
        log.info("------------------------ file(s) delete finished -----------------------");

        return flag;
    }

    // open connection to the remote server.
    public static void openConnection() throws SftpException {
        sftp.connect();
    }

    // disconnect from the remote server.
    public static void closeConnection() {
        sftp.disconnect();
    }
}

这是我得到的错误,因此jscape sftp对我不起作用。

2014-11-29 09:00:04 ERROR MainEntry:47 - There was error and message is java.lang.RuntimeException: Your Secure FTP Factory license expired on Sun Nov 23 00:31:21 CST 2014. Please v
isit http://www.jscape.com to purchase a licensed copy.
2014-11-29 09:00:04 ERROR MainEntry:48 - There was error and stack-trace is [Ljava.lang.StackTraceElement;@fdb00d

非常感谢任何帮助。如果有人知道其他库删除非空文件夹,请在这里询问。

1 个答案:

答案 0 :(得分:7)

我能够以递归方式删除目录。谢谢@Rob

这里的代码将有助于任何人在那里寻找它。

public static void deleteDirectory(ChannelSftp sftp, String oldestBackup) throws SftpException {
    if (isDir(sftp, oldestBackup)) {
        sftp.cd(oldestBackup);
        Vector < LsEntry > entries = sftp.ls(".");
        for (LsEntry entry: entries) {
            deleteDirectory(sftp, entry.getFilename());
        }
        sftp.cd("..");
        sftp.rmdir(oldestBackup);
    } else {
        sftp.rm(oldestBackup);
    }
}

private static boolean isDir(ChannelSftp sftp, String entry) throws SftpException {
    return sftp.stat(entry).isDir();
}