无法使用 java 中的 jsch 将文件列表提取到远程服务器的目录

Unable to fetch list of files using jsch in java, to remote server's directory

我正在尝试使用 jsch 库将文件填充到远程服务器。目录结构类似于 '/home/myname/batch_run_dt=20180706/batchid=P20180706001/* '

所以基础目录,batch_run_dt,包含多个子目录(batchid=P20180706*),还有那些有文件。在远程服务器中以类似的方式维护该结构。我正在使用下面的代码来实现此功能。

    public class Runner{
    public static void main(String[] args) throws FileNotFoundException, IOException, SQLException, JSchException {
    Date todaydate = new Date();
    Calendar cal = Calendar.getInstance();
    cal.setTime(todaydate);
    cal.add(Calendar.DAY_OF_MONTH,-1);


 SimpleDateFormat ft = new SimpleDateFormat("yyyyMMdd");

 String date = ft.format(cal.getTime());
    logger.info("date :: " + date);
    // starting the process

        String partition = "batch_run_dt=" + date;
        getData(partition);
}    
 public static void getData(String partition)       throws JSchException {
    logger.info("Starting download data ");
    // get the destination location
    String destLoc = "/home/myname/" + partition;

    // get source location from data needs to import through sftp
    String srcLoc = "/home/myname/" + partition;

    download(partition, prop, destLoc, srcLoc);
    logger.info("Finished download prov data ");
}

public static void download(String partition, Properties prop, String destLoc, String srcLoc) throws JSchException {

    // creaating a sesssion for jsch
    Session session = NonProdRunner.createjschsession("username",
            "password", "host");
    // creating a channel from session
    Channel channel = session.openChannel("sftp");
    channel.connect();
    // creating sftp channel to connect
    ChannelSftp channelSftp = (ChannelSftp) channel;

    // create file object
    File dir = new File(destLoc);

    // check if file exists if exists, means we already have data
    // else create directory structure
    if (!dir.exists()) {
        dir.mkdirs();
    } else {
        logger.error("File already exist");
        new Exception("Destination already Exists");
    }

    logger.info("Created destination folders");

    // get all the files
    mget(channelSftp, srcLoc, destLoc);
    // close connection and session
    channelSftp.exit();
    session.disconnect();
    System.out.println("session disconnected");

}

private static Session createjschsession(String uname, String pwd, String host) throws JSchException {
    JSch jsch = new JSch();
    Session session = jsch.getSession(uname, host);
    session.setPassword(pwd);
    java.util.Properties config = new java.util.Properties();
    config.put("StrictHostKeyChecking", "no");
    session.setConfig(config);
    logger.info("Connecting Session");
    session.connect();
    logger.info("Connected Session");
    return session;

}

private static void mget(ChannelSftp channelSftp, String srcLoc, String destLoc) {
    try {
        // Setting the folder location of the external system as
        // configured
        // to download the file from
        logger.info("src loation :: " + srcLoc);
        channelSftp.cd(srcLoc);
        channelSftp.lcd(destLoc);
        logger.info("folder changed");
        Vector<LsEntry> dirlist = channelSftp.ls("*");
        logger.info("Got list of remote files");
        for (LsEntry entry : dirlist) {

            Vector<LsEntry> batchID = channelSftp.ls("*");


            for (LsEntry batchEntry : batchID) {

                String filename = batchEntry.getFilename();
                logger.info("Loading BatchID: " + filename);

                File dir = new File(destLoc+"/"+filename);
                dir.mkdirs();

                logger.info(destLoc+"/"+filename);
                System.out.println(destLoc+"/"+filename);

                channelSftp.cd(filename);

                logger.info("Entered into: " + filename);

                Vector<LsEntry> files = channelSftp.ls(srcLoc+"/"+filename+"/*");
                for (LsEntry file: files) {

                    logger.info(file.getFilename());

                    String partFiles = file.getFilename();

                    channelSftp.get(partFiles, destLoc+"/"+filename);//.get(partFiles, partFiles);

                }
            }

        }
        logger.info("File copy done");

    } catch (SftpException sftpException) {
        logger.error("Exception in sftp: " + sftpException.getMessage());
        System.exit(-1);
    }
}

}

但是我在加载第一个子文件夹后收到错误 Exception in sftp: No such file 。不确定我在这里遗漏了什么,请帮助解决这个问题。

由于您使用 ls *,您将获得文件和文件夹,然后将其用于 cd filename

可能性:

  • 这是一个文件
  • 用户没有正确的权限

检查文档以确认问题:

ChannelSftp.cd

SftpException - if the named path does not indicate a directory, if it is not accessible by the user, or some other problem occurs.

另一个可能的问题是,如果您尝试移动到像这样的文件夹:

cd /root/folder*

其中结构提出了不止一个结果:

/root/folderA
/root/folderB

由于命令可能给出结果:folderAfolderB,您将得到一个异常:

Exception in sftp:/root/folder* is not unique: