使用Java从远程服务器复制具有特定扩展名的所有文件 [英] Copy all files with specific extension from remote server with Java

查看:75
本文介绍了使用Java从远程服务器复制具有特定扩展名的所有文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我正在尝试创建一个小脚本,该脚本可让我通过sftp将具有特定扩展名的所有文件从远程linux计算机复制到本地计算机.

Hey everybody I'm trying to create a small script that will let me copy all files with a specific extension from a remote linux machine to my local machine through sftp.

这是我到目前为止的代码,如果我给出完整路径,则可以使用Jsch将一个文件从远程计算机复制到本地计算机.

This is the code I have so far, which lets me copy one file from the remote machine to my local machine, using Jsch, if I give the full path.

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"的所有文件复制并放置在用户定义的文件夹中.

I'd like all of the files that have the extension ".jpg" in a specific folder to be copied and place in a folder the user defines.

我尝试过:

sftpChannel.get(copyFrom + "*.jpg", copyTo);

哪个没有用,我知道我应该使用类似的东西:

Which did not work, I know I should use something like:

pathname.getName().endsWith("." + fileType)

但是我不确定如何用sftpChannel实现它.

But I'm not sure how to implement it with sftpChannel.

推荐答案

您必须使用sftpChannel.ls("Path to dir");,它将返回给定路径中的文件列表作为矢量,并且您必须迭代该矢量以下载每个文件sftpChannel.get();

You have to use sftpChannel.ls("Path to dir"); which will returns list of files in the given path as a vector and you have to iterate on the vector to download each file 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"); 

        }
    }

这篇关于使用Java从远程服务器复制具有特定扩展名的所有文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆