背景
因为安全原因,需要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)
准备阶段
需要引用第三方类库Tamir.SharpSSH.dll。链接地址(http://www.tamirgal.com/blog/page/SharpSSH.aspx)
我下载的是(Download binaries: SharpSSH-1.1.1.13.bin.zip)DotNetSSH 文件
把Tamir.SharpSSH.dll加载到项目中。
实现阶段
增加一个类SFTPHelper
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
这里我只实现了下载的功能,上传的功能修改后加上。今天暂时不加。
发现C#转VB面向对象这块不熟悉,所以实现接口那里纠结了一下,后面还好自己乱敲了几个回车就好了,真想说,居然乱敲也行
调用部分
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在代码里面,不过这样灵活一些。