如何使用SFTPClient从远程服务器下载特定文件?

时间:2013-06-03 08:53:48

标签: java pattern-matching sftp

我是SFTP Server的新手。我尝试使用SFTP服务器上传和下载文件。这是一个独立的应用程序。 从远程服务器下载文件到本地服务器时,我可以使用以下SFTP代码下载指定路径下的文件列表

sftp.lcd(details.get("LOCAL_DIR"));
sftp.cd(details.get("REMOTE_DIR"));

List<SftpFile> remoteFiles = sftp.ls();
for(int i = 0 ; i < remoteFiles.size(); ++i) {
    if(remoteFiles.get(i).isFile()) {
        String remoteFile = remoteFiles.get(i).getFilename();
        sftp.get(remoteFile);
        System.out.println("[SFTPOperations][downLoad] Downloaded: " + remoteFile);
        System.out.println("Remote File: " + remoteFile);
        System.out.println("Remote Archive Dir: " + details.get("REMOTE_ARCHIVE_DIR"));
        sftp.rename(remoteFiles.get(i).getFilename(), details.get("REMOTE_ARCHIVE_DIR"));
        System.out.println("[SFTPOperations][downLoad] Archived: " + remoteFile);
    }
}

通过使用上面的代码,它将所有文件从远程目录下载到本地目录。在这里,我想从远程目录下载匹配给定模式的文件。

E.g。模式:Query*.txt

我想下载以Query开头的文本文件

请建议如何进行上述操作。

2 个答案:

答案 0 :(得分:1)

你试过这个吗?

if (remoteFile.endsWith(".txt")) {
  sftp.get(remoteFile);
}

但是,根据您要评估的模式,您可以使用某种常规表达来验证文件名是否有效

答案 1 :(得分:0)

using Renci.SshNet;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Configuration;

namespace FileDownload
{
    class Program
    {
        static void Main(string[] args)
        {
            string date = DateTime.Now.ToShortDateString();
            using (StreamWriter writer = File.CreateText(ConfigurationManager.AppSettings["localDirectory"]+"U2000_CSVFile_Fetching_log_"+date+".txt"))
            {   
                try
                {

                    string host = ConfigurationManager.AppSettings["host"];
                    string username = ConfigurationManager.AppSettings["username"];

                    string password = ConfigurationManager.AppSettings["password"];

                    string remoteDirectory = ConfigurationManager.AppSettings["remoteDirectory"]+date+"/";

                    Console.WriteLine(password);

                    string localDirectory = ConfigurationManager.AppSettings["localDirectory"] + date;
                    System.IO.Directory.CreateDirectory(localDirectory);
                    Console.WriteLine(localDirectory);
                    using (var sftp = new SftpClient(host, username, password))
                    {


                        sftp.Connect();

                        writer.WriteLine("Connection OK");

                        var files = sftp.ListDirectory(remoteDirectory);

                        foreach (var file in files)
                        {
                            string remoteFileName = file.Name;

                            writer.WriteLine(file.Name);

                            if ((!file.Name.StartsWith(".")))
                            {
                               using (Stream file1 = File.OpenWrite(localDirectory + remoteFileName))
                                {
                                    writer.WriteLine(file.Name + " Download Started");

                                    sftp.DownloadFile(remoteDirectory + remoteFileName, file1);

                                    writer.WriteLine(file.Name + " Downloaded Successfully_"+date);
                                }
                            }

                        }

                    }

                }
                catch (Exception ex)
                {
                    writer.WriteLine(ex.Message);
                }


            }
            Console.WriteLine("U2000 .csv files fetching Scheduler is Running");
            Console.ReadLine();
        }
    }
}