0

こんにちは、特定の拡張子を持つすべてのファイルをリモート 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 で実装する方法がわかりません。

4

1 に答える 1

1

指定されたパス内のファイルのリストをベクトルとして返す whichを使用する必要sftpChannel.ls("Path to dir");があり、ベクトルを反復処理して各ファイルをダウンロードする必要がありますsftpChannel.get();

Vector<ChannelSftp.LsEntry> list = sftpChannel .ls("."); 
    // iterate through objects in list, and check for extension
    for (ChannelSftp.LsEntry listEntry : list) {
            sftpChannel.get(listEntry.getFilename(), "fileName"); 

        }
    }
于 2013-09-16T15:12:58.763 に答える