こんにちは、特定の拡張子を持つすべてのファイルをリモート Linux マシンからローカル マシンに sftp 経由でコピーできる小さなスクリプトを作成しようとしています。
これは私がこれまでに作成したコードで、完全なパスを指定すると、Jsch を使用してリモート マシンからローカル マシンに 1 つのファイルをコピーできます。
package transfer;
import com.jcraft.jsch.*;
import java.io.File;
import java.io.FilenameFilter;
import java.util.Scanner;
public class CopyFromServer {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
System.out.println("Please enter the hostname or ip of the server on which the ctk files can be found: ");
String hostname = sc.nextLine();
System.out.println("Please enter your username: ");
String username = sc.nextLine();
System.out.println("Please enter your password: ");
String password = sc.nextLine();
System.out.println("Please enter the location where your files can be found: ");
String copyFrom = sc.nextLine();
System.out.println("Please enter the location where you want to place your files: ");
String copyTo = sc.nextLine();
JSch jsch = new JSch();
Session session = null;
try {
session = jsch.getSession(username, hostname, 22);
session.setConfig("StrictHostKeyChecking", "no");
session.setPassword(password);
session.connect();
Channel channel = session.openChannel("sftp");
channel.connect();
ChannelSftp sftpChannel = (ChannelSftp) channel;
sftpChannel.get(copyFrom, copyTo);
sftpChannel.exit();
session.disconnect();
} catch (JSchException e) {
e.printStackTrace();
} catch (SftpException e) {
e.printStackTrace();
}
}
}
特定のフォルダにある拡張子「.jpg」を持つすべてのファイルをコピーして、ユーザーが定義したフォルダに配置したいと考えています。
私はもう試した:
sftpChannel.get(copyFrom + "*.jpg", copyTo);
これは機能しませんでした。次のようなものを使用する必要があることはわかっています。
pathname.getName().endsWith("." + fileType)
しかし、sftpChannel で実装する方法がわかりません。