我正在尝试使用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);
}
}
}
但是在加载第一个子文件夹后,我得到一个错误,该错误是sftp中的 异常:没有这样的文件 。不确定我在这里缺少什么,请协助解决这个问题。
答案 0 :(得分:1)
由于您使用ls *
,因此将获得文件和文件夹,然后将其用于cd filename
。
可能性:
检查文档以确认问题:
SftpException-如果命名路径未指示目录,用户无法访问目录或发生其他问题。
另一个可能的问题是,如果您尝试移动到类似:的文件夹中,
cd /root/folder*
该结构提出了多个结果:
/root/folderA
/root/folderB
由于该命令可以给出结果:folderA
和folderB
,因此您将获得异常:
Exception in sftp:/root/folder* is not unique: