做的一个项目,大多数地方都是使用ftp,就有一小撮文件很重要,想用sftp,服务器上sftp已建立,foxtable项目已经外部引用了Tamir.SharpSSH.dll。
然后我就想是不是要在全局代码里加代码,我在网上找到c#代码,vb代码如下,添加进去无法保存,请专家给予指引
以下内容为程序代码:
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