Public Class SFTPHelper
Private m_session As Session
Private m_channel As Channel
Private m_sftp As ChannelSftp
Public Sub New(ByVal host As String, ByVal user As String, ByVal pwd As String)
Dim arr() As String = host.Split(":")
Dim ip As String = arr(0)
Dim port As Integer = 22
If (arr.Length > 1) Then
port = Int32.Parse(arr(1))
End If
Dim jsch As JSch = New JSch()
m_session = jsch.getSession(user, ip, port)
Dim ui As MyUserInfo = New MyUserInfo()
ui.setPassword(pwd)
m_session.setUserInfo(ui)
End Sub
'SFTP获取文件
Public Function GetInfo(ByVal remotePath As String, ByVal localPath As String) As Boolean
Try
Dim src As Tamir.SharpSsh.java.String = New Tamir.SharpSsh.java.String(remotePath)
Dim dst As Tamir.SharpSsh.java.String = New Tamir.SharpSsh.java.String(localPath)
m_sftp.get(src, dst)
Return True
Catch
Return False
End Try
End Function
'SFTP连接状态
Public Property Connected As Boolean
Get
Return m_session.isConnected()
End Get
Set(ByVal value As Boolean)
End Set
End Property
'连接SFTP
Public Function Connect() As Boolean
Dim flag As Boolean = False
Try
If (Not Connected()) Then
m_session.connect()
m_channel = m_session.openChannel("sftp")
m_channel.connect()
m_sftp = m_channel
flag = True
End If
Catch
flag = False
End Try
Return flag
End Function
'断开SFTP
Public Sub Disconnect()
If (Connected()) Then
m_channel.disconnect()
m_session.disconnect()
End If
End Sub
'登录验证信息
Public Class MyUserInfo
Implements UserInfo
Dim passwd As String
Public Sub setPassword(ByVal ppasswd As String)
passwd = ppasswd
End Sub
Public Function getPassphrase() As String Implements Tamir.SharpSsh.jsch.UserInfo.getPassphrase
Return Nothing
End Function
Public Function getPassword() As String Implements Tamir.SharpSsh.jsch.UserInfo.getPassword
Return passwd
End Function
Public Function promptPassphrase(ByVal message As String) As Boolean Implements Tamir.SharpSsh.jsch.UserInfo.promptPassphrase
Return True
End Function
Public Function promptPassword(ByVal message As String) As Boolean Implements Tamir.SharpSsh.jsch.UserInfo.promptPassword
Return True
End Function
Public Function promptYesNo(ByVal message As String) As Boolean Implements Tamir.SharpSsh.jsch.UserInfo.promptYesNo
Return True
End Function
Public Sub showMessage(ByVal message As String) Implements Tamir.SharpSsh.jsch.UserInfo.showMessage
End Sub
End Class
End Class