VB實現SFTP下載和上傳的功能

背景

  由於安全緣由,須要SFTP協議(sftp是Secure File Transfer Protocol的縮寫,安全文件傳送協議。能夠爲傳輸文件提供一種安全的加密方法。sftp 與 ftp 有着幾乎同樣的語法和功能。SFTP 爲 SSH的一部份,是一種傳輸檔案至 Blogger 伺服器的安全方式。其實在SSH軟件包中,已經包含了一個叫做SFTP(Secure File Transfer Protocol)的安全文件傳輸子系統,SFTP自己沒有單獨的守護進程,它必須使用sshd守護進程(端口號默認是22)來完成相應的鏈接操做,因此從某種意義上來講,SFTP並不像一個服務器程序,而更像是一個客戶端程序。SFTP一樣是使用加密傳輸認證信息和傳輸的數據,因此,使用SFTP是很是安全的。可是,因爲這種傳輸方式使用了加密/解密技術,因此傳輸效率比普通的FTP要低得多,若是您對網絡安全性要求更高時,可使用SFTP代替FTP)java

準備階段

  須要引用第三方類庫Tamir.SharpSSH.dll。連接地址(http://www.tamirgal.com/blog/page/SharpSSH.aspx安全

     我下載的是(Download binaries: SharpSSH-1.1.1.13.bin.zipDotNetSSH 文件服務器

  把Tamir.SharpSSH.dll加載到項目中。網絡

實現階段

  增長一個類SFTPHelpersession

  

  1 Imports Tamir.SharpSsh.jsch
  2 
  3 Public Class SFTPHelper
  4     Private m_session As Session
  5     Private m_channel As Channel
  6     Private m_sftp As ChannelSftp
  7 
  8     Public Sub New(ByVal host As String, ByVal user As String, ByVal pwd As String)
  9         Dim arr() As String = host.Split(":")
 10         Dim ip As String = arr(0)
 11         Dim port As Integer = 22
 12         If (arr.Length > 1) Then
 13             port = Int32.Parse(arr(1))
 14         End If
 15 
 16         Dim jsch As JSch = New JSch()
 17         m_session = jsch.getSession(user, ip, port)
 18         Dim ui As MyUserInfo = New MyUserInfo()
 19         ui.setPassword(pwd)
 20         m_session.setUserInfo(ui)
 21     End Sub
 22 
 23     'SFTP獲取文件        
 24     Public Function GetInfo(ByVal remotePath As String, ByVal localPath As String) As Boolean
 25         Try
 26             Dim src As Tamir.SharpSsh.java.String = New Tamir.SharpSsh.java.String(remotePath)
 27             Dim dst As Tamir.SharpSsh.java.String = New Tamir.SharpSsh.java.String(localPath)
 28             m_sftp.get(src, dst)
 29             Return True
 30         Catch
 31             Return False
 32         End Try
 33     End Function
 34     'SFTP鏈接狀態        
 35     Public Property Connected As Boolean
 36         Get
 37             Return m_session.isConnected()
 38         End Get
 39         Set(ByVal value As Boolean)
 40 
 41         End Set
 42     End Property
 43 
 44     '鏈接SFTP        
 45     Public Function Connect() As Boolean
 46         Dim flag As Boolean = False
 47         Try
 48             If (Not Connected()) Then
 49                 m_session.connect()
 50                 m_channel = m_session.openChannel("sftp")
 51                 m_channel.connect()
 52                 m_sftp = m_channel
 53                 flag = True
 54             End If
 55         Catch
 56             flag = False
 57         End Try
 58         Return flag
 59     End Function
 60 
 61     '斷開SFTP        
 62     Public Sub Disconnect()
 63 
 64         If (Connected()) Then
 65             m_channel.disconnect()
 66             m_session.disconnect()
 67         End If
 68     End Sub
 69 
 70     '登陸驗證信息        
 71     Public Class MyUserInfo
 72         Implements UserInfo
 73 
 74         Dim passwd As String
 75 
 76         Public Sub setPassword(ByVal ppasswd As String)
 77             passwd = ppasswd
 78         End Sub
 79         Public Function getPassphrase() As String Implements Tamir.SharpSsh.jsch.UserInfo.getPassphrase
 80             Return Nothing
 81         End Function
 82         Public Function getPassword() As String Implements Tamir.SharpSsh.jsch.UserInfo.getPassword
 83             Return passwd
 84         End Function
 85 
 86         Public Function promptPassphrase(ByVal message As String) As Boolean Implements Tamir.SharpSsh.jsch.UserInfo.promptPassphrase
 87             Return True
 88         End Function
 89         Public Function promptPassword(ByVal message As String) As Boolean Implements Tamir.SharpSsh.jsch.UserInfo.promptPassword
 90             Return True
 91         End Function
 92         Public Function promptYesNo(ByVal message As String) As Boolean Implements Tamir.SharpSsh.jsch.UserInfo.promptYesNo
 93             Return True
 94         End Function
 95         Public Sub showMessage(ByVal message As String) Implements Tamir.SharpSsh.jsch.UserInfo.showMessage
 96 
 97         End Sub
 98     End Class
 99 
100 End Class

     這裏我只實現了下載的功能,上傳的功能修改後加上。今天暫時不加。ssh

    發現C#轉VB面向對象這塊不熟悉,因此實現接口那裏糾結了一下,後面還好本身亂敲了幾個回車就行了,真想說,竟然亂敲也行ide

    調用部分      ui

 1  Dim RedirectURL As String = loMicUi.GetApKeyValue("RedirectURL")
 2             Dim strLoaclfilePath As String = loMicUi.GetApKeyValue("LoaclFile")
 3 
 4             Dim sftp As SFTPHelper = New SFTPHelper(loMicUi.GetApKeyValue("HostIP"), loMicUi.GetApKeyValue("UserName"), loMicUi.GetApKeyValue("Password"))
 5             sftp.Connect()
 6             Dim strSourceFilePath As String = e.Item.Cells(12).Text
 7             Dim strLocalFileNameList() As String = e.Item.Cells(12).Text.Split("\")
 8             Dim strLocalFileName As String = strLocalFileNameList(strLocalFileNameList.Length - 1)
 9             If strSourceFilePath.Substring(0, 2) <> "\\" Then
10                 If strSourceFilePath(0) = "\" Then
11                     strSourceFilePath = "\" & strSourceFilePath
12                 Else
13                     strSourceFilePath = "\\" & strSourceFilePath
14                 End If
15             End If
16             If Not sftp.GetInfo(strSourceFilePath, strLoaclfilePath) Then
17                 Throw New Exception("Can not find file in SFTP path[" & strSourceFilePath & "]")
18             End If
19             sftp.Disconnect()
20             strLoaclfilePath = strLoaclfilePath & "\" & strLocalFileName
21 
22             If File.Exists(strLoaclfilePath) Then
23                 Response.Redirect(RedirectURL & strLocalFileName)
24             Else
25                 'Can not find path 
26                 Throw New Exception("Can not find path[" & strLocalFileName & "]")
27             End If
調用部分

大體上是可以下載下來,也能打開了,另外host,username,password,SFTP上的文件路徑以及下載到本地的路徑,我都是須要本身在config裏面配置,其實也能夠直接hardcode在代碼裏面,不過這樣靈活一些。加密

相關文章
相關標籤/搜索