java - 无法在 java 中使用 jsch 获取文件列表到远程服务器的目录
问题描述
我正在尝试使用 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中加载第一个子文件夹后出现错误:没有这样的文件。不确定我在这里缺少什么,请帮助解决这个问题。
解决方案
由于您使用ls *
,您将获得文件和文件夹,然后您将其用于cd filename
.
可能性:
- 这是一个文件
- 用户没有正确的权限
检查文档以确认问题:
SftpException - 如果命名路径不指示目录,如果用户无法访问,或者出现其他问题。
另一个可能的问题是,如果您尝试移动到以下文件夹:
cd /root/folder*
其中结构提出了多个结果:
/root/folderA
/root/folderB
由于该命令可能会给出结果:folderA
并且folderB
,您将得到一个异常:
Exception in sftp:/root/folder* is not unique:
推荐阅读
- python - Django HTML 变量中的图像字段
- c++ - 练习验证一个单词是否==它是反向的
- javascript - 用户输入创建弹出图像
- c++ - 如何从数组元素的总和中减去最小元素
- discord.py - 任何人都可以使用清除命令
- asp.net-mvc - 如何使用 ASP.NET MVC 项目处理 HTTP 404
- c - 目标是将两个字符串读入 2 个不同的变量。然后我们统计每个字符串中“C”的个数并输出结果
- hibernate - “内部服务器错误:无法获取当前线程的事务同步会话”即使添加了@Transaction
- html - 如何在scss中使用rgb颜色?
- azure - azure 创建交互式服务