0

我在我的 SSIS 2012 包中创建了一个脚本任务,它需要从 FTP 服务器抓取文件并将它们存储在本地服务器的硬盘上。我在脚本任务s中编写的代码如下:

Public Sub Main()
    '
    ' Add your code here
    '
    Const localFile As String = "C:\InboundOrders\*.*"
    Const remoteFile As String = "/remote/folder/filename.pgp"
    Const host As String = "ftp://ftp1.commerceub.com"
    Const user As String = "ourusername"
    Const pw As String = "ourpassword"

    Dim URI As String = host & remoteFile
    Dim ftp As System.Net.FtpWebRequest = CType(FtpWebRequest.Create(URI), 
    FtpWebRequest)

    ftp.Credentials = New System.Net.NetworkCredential(user, pw)
    ftp.KeepAlive = False
    ftp.UseBinary = True
    ftp.Method = System.Net.WebRequestMethods.Ftp.DownloadFile

    Using response As System.Net.FtpWebResponse = CType(ftp.GetResponse, 
    System.Net.FtpWebResponse)
        Using responseStream As IO.Stream = response.GetResponseStream
            Using fs As New IO.FileStream(localFile, IO.FileMode.Create)
                Dim buffer(2047) As Byte
                Dim read As Integer = 0
                Do
                    read = responseStream.Read(buffer, 0, buffer.Length)
                    fs.Write(buffer, 0, read)
                Loop Until read = 0
                responseStream.Close()
                fs.Flush()
                fs.Close()
            End Using
            responseStream.Close()
        End Using
        response.Close()
    End Using
    Dts.TaskResult = ScriptResults.Success
End Sub

当我运行包时,我收到一条错误消息:

DTS Script Task has encountered an exception in user code.
   at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, 
   Signature sig, Boolean constructor)
   at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, 
   Binder binder, Object[] parameters, CultureInfo culture)
   at System.RuntimeType.InvokeMember(String name, BindingFlags bindingFlags, Binder 
   binder, Object target, Object[] providedArgs, ParameterModifier[] modifiers, 
   CultureInfo culture, String[] namedParams)
   at System.Type.InvokeMember(String name, BindingFlags invokeAttr, Binder binder,    
   Object target, Object[] args, CultureInfo culture)
   at Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTATaskScriptingEngine.ExecuteScript() 

我应该怎么做才能纠正上述错误?感谢您的帮助和指导。

更新:我在我的代码中指定了一个实际位于 FTP 服务器上的文件。我收到同样的错误。我应该采取不同的方法吗?

4

0 回答 0