2015-02-05 2 views
0

SSIS 스크립트 태스크를 사용하여 FTP에서 파일을 다운로드하려고하는데 ftpWebRequest 클래스를 사용하여 FTP에서 파일을 다운로드하는 코드를 작성했습니다. 그러나 sFTP에서 다운로드하라는 메시지가 표시 될 수도 있습니다. 많은 사람들이 WinSCP 어셈블리가 sFTP 파일을 다운로드하는 가장 좋은 방법이라고 제안하고 있기 때문에 WinSCP 클래스가있는 SSIS 스크립트를 사용하여 파일을 다운로드 할 수있는 몇 가지 예가 나와 있습니다.SSIS 스크립트 태스크를 사용하여 FTP에서 파일 다운로드

PS : 사람들이 유일한 옵션은 WinSCP에 같은 라이브러리를 사용하는 것입니다 말했듯이 내가 Vb.net

+0

참고 예제의 소스 코드 당신 SFTP를하기보다는, FTPS 사이트에서 다운로드 끝날 경우 , 당신은 가벼운 [Alex FTPS Client] (https://ftps.codeplex.com/) – sorrell

답변

1

예에 새로운 오전.

WinSCP에 웹 사이트 여기 예를 살펴보세요 - 여기 http://winscp.net/eng/docs/script_vbnet_robust_example

Imports System 
Imports System.IO 
Imports System.Diagnostics 
Imports System.Xml 
Imports System.Xml.XPath 
Imports System.Configuration.ConfigurationManager 

Public Class SFTP 
    ' SFTP support, built on WinSCP 
    Public Shared Function PutSFTP(ByRef filename As String, ByRef remotehost As String, ByRef username As String, ByRef password As String, _ 
          Optional ByVal outfilename As String = Nothing, Optional ByVal output As String = Nothing, 
          Optional ByRef errmsg As String = Nothing) As Boolean 

     ' Run hidden WinSCP process 
     Dim winscp As Process = New Process() 
     Dim logname As String = Path.ChangeExtension(Path.GetTempFileName, "xml") 
     With winscp.StartInfo 
      ' SFTPExecutable needs to be defined in app.config to point to winscp.com 
      Try 
       .FileName = AppSettings("SFTPExecutable") 
       If .FileName Is Nothing OrElse .FileName.Length = 0 Then Throw (New Exception("from PutSFTP: SFTPExecutable not set in config file.")) 
      Catch ex As Exception 
       errmsg = ex.Message 
       Return False 
      End Try 
      .Arguments = "/xmllog=" + logname 
      .UseShellExecute = False 
      .RedirectStandardInput = True 
      .RedirectStandardOutput = True 
      .CreateNoWindow = True 
     End With 
     Try 
      winscp.Start() 
     Catch ex As Exception 
      errmsg = "from PutSFTP: Could not run the WinSCP executable " & winscp.StartInfo.FileName & Environment.NewLine & ex.Message 
      Return False 
     End Try 

     ' Feed in the scripting commands 
     With winscp.StandardInput 
      .WriteLine("option batch abort") 
      .WriteLine("option confirm off") 
      .WriteLine("open sftp://" & username & ":" & password & "@" & remotehost & "/") 
      If outfilename Is Nothing Then .WriteLine("put " & filename) Else .WriteLine("put " & filename & " """ & outfilename & """") 
      .Close() 
     End With 
     If output IsNot Nothing Then output = winscp.StandardOutput.ReadToEnd() 

     ' Wait until WinSCP finishes 
     winscp.WaitForExit() 

     ' Parse and interpret the XML log 
     ' (Note that in case of fatal failure the log file may not exist at all) 
     If Not File.Exists(logname) Then 
      errmsg = "from PutSFTP: The WinSCP executable appears to have crashed." 
      Return False 
     End If 

     Dim log As XPathDocument = New XPathDocument(logname) 
     Dim ns As XmlNamespaceManager = New XmlNamespaceManager(New NameTable()) 
     ns.AddNamespace("w", "http://winscp.net/schema/session/1.0") 
     Dim nav As XPathNavigator = log.CreateNavigator() 

     ' Success (0) or error? 
     Dim status As Boolean = (winscp.ExitCode = 0) 
     If Not status Then 
      errmsg = "from PutSFTP: There was an error transferring " & filename & "." 
      ' See if there are any messages associated with the error 
      For Each message As XPathNavigator In nav.Select("//w:message", ns) 
       errmsg &= Environment.NewLine & message.Value 
      Next message 
     End If 

     Try 
      My.Computer.FileSystem.DeleteFile(logname) 
     Catch ex As Exception 
      ' at least we tried to clean up 
     End Try 

     Return status 
    End Function 
End Class 
관련 문제