1

请考虑下面的代码,它适用于小文件(小于 50 MB),但 100 MB 或更大的文件会崩溃。并给出以下错误。

“无法将数据写入传输连接:无法对套接字执行操作,因为系统缺少足够的缓冲区空间或队列已满。”

'Method
Private Sub Upload(ByVal source As String, ByVal target As String, ByVal credential As NetworkCredential)
    Dim request As FtpWebRequest = DirectCast(WebRequest.Create(target), FtpWebRequest)
    request.Method = WebRequestMethods.Ftp.UploadFile
    request.Credentials = credential
    request.Proxy = Nothing
    Dim reader As New FileStream(source, FileMode.Open)
    Dim buffer(Convert.ToInt32(reader.Length - 1)) As Byte
    reader.Read(buffer, 0, buffer.Length)
    reader.Close()
    request.ContentLength = buffer.Length
    Dim stream As Stream = request.GetRequestStream
    stream.Write(buffer, 0, buffer.Length)
    stream.Close()
    Dim response As FtpWebResponse = DirectCast(request.GetResponse, FtpWebResponse)
    response.Close()
End Sub

'Calling Function
Dim credential As New NetworkCredential("FTPUserName", "FTPPassword")

Upload(FileName.Zip, "ftp://abc.com/Location/FileName.Zip", credential)

请告诉我我错过了什么。

4

1 回答 1

1

请使用此脚本。效果很好,我已经用 200+ GB 的文件对其进行了测试。

Private Sub Upload(ByVal sourceFiles() As String)
    Dim cm As ConnectionManager = Dts.Connections.Add("FTP")
    cm.Properties("ServerName").SetValue(cm, Dts.Variables("User::FTPServerName").Value.ToString())
    cm.Properties("ServerUserName").SetValue(cm, Dts.Variables("User::FTPUserName").Value.ToString())
    cm.Properties("ServerPassword").SetValue(cm, Dts.Variables("User::FTPPassword").Value.ToString())
    cm.Properties("ServerPort").SetValue(cm, "21")
    cm.Properties("Timeout").SetValue(cm, "0")
    cm.Properties("ChunkSize").SetValue(cm, "1000")
    cm.Properties("Retries").SetValue(cm, "1")
    Dim ftp As FtpClientConnection = New FtpClientConnection(cm.AcquireConnection(Nothing))
    ftp.Connect()
    ftp.SendFiles(sourceFiles, Dts.Variables("User::FTPFolder").Value.ToString(), True, False)
    ftp.Close()
End Sub

谢谢

于 2013-01-21T22:41:08.693 回答