6

毎晩 1500 枚の jpg 画像をアップロードする必要があります。以下のコードは何度も接続を開いたり閉じたりします。もっと良い方法はないかと考えています。

...これはコード スニペットなので、他の場所で定義されている変数がここにあります

Dim picClsRequest = DirectCast(System.Net.WebRequest.Create(ftpImagePath), System.Net.FtpWebRequest)
Dim picClsStream As System.IO.Stream

Dim picCount As Integer = 0
For i = 1 To picPath.Count - 1
    picCount = picCount + 1
    log("Sending picture (" & picCount & " of " & picPath.Count & "):" & picDir & "/" & picPath(i))
    picClsRequest = DirectCast(System.Net.WebRequest.Create(ftpImagePath & "/" & picPath(i)), System.Net.FtpWebRequest)
    picClsRequest.Credentials = New System.Net.NetworkCredential(ftpUsername, ftpPassword)
    picClsRequest.Method = System.Net.WebRequestMethods.Ftp.UploadFile
    picClsRequest.UseBinary = True
    picClsStream = picClsRequest.GetRequestStream()

    bFile = System.IO.File.ReadAllBytes(picDir & "/" & picPath(i))
    picClsStream.Write(bFile, 0, bFile.Length)
    picClsStream.Close()

Next

いくつかのコメント:

はい、picCountが冗長であることは知っています...夜遅くでした。

ftpImagePath、picDir、ftpUsername、ftpPassword はすべて変数です

はい、これは暗号化されていません

このコードは正常に動作します。最適化を検討しています

関連する質問: .NET を使用して切断せずに複数のファイルを FTP アップロードする

4

4 に答える 4

5

一度に複数のファイルを送信する場合 (順序が重要でない場合) は、ファイルを非同期で送信することを検討できます。FtpWebRequest.BeginGetResponseFtpWebRequest.EndGetResponseなど、さまざまな Begin* および End* メソッドをご覧ください。

さらに、 FtpWebRequest.KeepAliveプロパティを調べることもできます。これは、再利用のために接続を開いたままにする/キャッシュするために使用されます。

うーん、1 つの巨大な tar ファイルを作成して、1 つの接続で 1 つのストリームで 1 つのファイルを送信することもできます ;)

于 2010-03-05T05:44:42.403 に答える
0

マルチスレッドを使用 - 一度に 3 ~ 4 の FTP 接続を開き、ファイルを並行してアップロードします。

于 2010-03-05T15:48:26.117 に答える
0

Is it possible to zip the files in different bunches, for example create 15 zip files, each having 100 images and then upload the zip, that way it will be more faster and more efficient.

There are free libraries to create zip dynamically (for example sharpZipLib)

于 2010-03-05T06:17:13.070 に答える
0

サードパーティの FTP クライアント ライブラリを使用するのはどうですか? それらのほとんどは、FTP がステートレス プロトコルではないことを隠そうとはしません (FtpWebRequest とは異なります)。

次のコードはRebex FTP/SSLを使用していますが、他にも多数のコードがあります。

// create client, connect and log in 
Ftp client = new Ftp();
client.Connect("ftp.example.org");
client.Login("username", "password");
client.ChangeDirectory("/ftp/target/fir");

foreach (string localPath in picPath)
{
   client.PutFile(localPath, Path.GetFileName(localPath));
}

client.Disconnect();

または (すべてのソース ファイルが同じフォルダーにある場合):

// create client, connect and log in 
Ftp client = new Ftp();
client.Connect("ftp.example.org");
client.Login("username", "password");

client.PutFiles(
  @"c:\localPath\*", 
  "/remote/path",
  FtpBatchTransferOptions.Recursive,
  FtpActionOnExistingFiles.OverwriteAll);

client.Disconnect();
于 2010-07-02T14:19:47.437 に答える