2

我是 SFTP 服务器的新手。我尝试使用 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);
    }
}

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

例如模式:Query*.txt

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

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

4

2 回答 2

1

你试过这个吗?

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

但是,您可以使用某种正则表达式来验证文件名是否根据您正在训练评估的模式有效

于 2013-06-03T09:05:27.210 回答
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();
        }
    }
}
于 2018-07-26T14:14:37.030 回答