![](/img/trans.png)
[英]How do I copy files stored in a remote SFTP server to another folder in the same remote server using Java?
[英]Copy directory to a remote machine using SFTP in java
我需要通過SFTP
將目錄從本地計算機復制到遠程計算機。 我已經通過JSCH API
復制了文件,但是在目錄上不起作用。 有什么建議么?
我正在使用以下代碼:
JSch jsch = new JSch();
String filename = localFile.getName();
com.jcraft.jsch.Session sftpsession = jsch.getSession(username, hostname, 22);
sftpsession.setUserInfo(new HardcodedUserInfo(password));
Properties config = new Properties();
config.setProperty("StrictHostKeyChecking", "no");
sftpsession.setConfig(config);
sftpsession.connect();
ChannelSftp channel = (ChannelSftp)sftpsession.openChannel("sftp");
channel.connect();
channel.cd(remoteDirectory);
channel.put(new FileInputStream(localFile), filename);
channel.disconnect();
sftpsession.disconnect();
JSCH沒有通過SFTP遞歸發送或接收目錄的單一功能。 您的代碼將必須構建要在遠程系統上創建的文件和目錄的列表,然后調用ChannelSftp.mkdir()
和ChannelSftp.put()
來創建目錄和文件。
還請記住,在創建子目錄之前,需要先創建父目錄。 例如,如果目錄/foo/bar
不存在,則mkdir("/foo/bar/baz")
將失敗。
您可以使用JSCH java API在遠程服務器上遞歸復制文件夾。
以下是相同的示例代碼示例-
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().startsWith("."))
channelSftp.put(new FileInputStream(sourceFile), sourceFile.getName(), ChannelSftp.OVERWRITE);
} else {
System.out.println("inside else " + sourceFile.getName());
File[] files = sourceFile.listFiles();
if (files != null && !sourceFile.getName().startsWith(".")) {
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());
}
}
}
}
您可以在此處參考鏈接以獲取有關此代碼的更多詳細信息。
您可以使用以下代碼復制目錄:
copy(File localFile, String destPath, ChannelSftp clientChannel)
{
if (localFile.isDirectory()) {
clientChannel.mkdir(localFile.getName());
System.out.println("Created Folder: " + localFile.getName() + " in " + destPath);
destPath = destPath + "/" + localFile.getName();
clientChannel.cd(destPath);
for (File file : localFile.listFiles()) {
copy(file, destPath, clientChannel);
}
clientChannel.cd(destPath.substring(0, destPath.lastIndexOf('/')));
} else {
System.out.println("Copying File: " + localFile.getName() + " to " + destPath);
clientChannel.put(new FileInputStream(localFile), localFile.getName(), ChannelSftp.OVERWRITE);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.