- ubuntu12.04环境下使用kvm ioctl接口实现最简单的虚拟机
- Ubuntu 通过无线网络安装Ubuntu Server启动系统后连接无线网络的方法
- 在Ubuntu上搭建网桥的方法
- ubuntu 虚拟机上网方式及相关配置详解
CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.
这篇CFSDN的博客文章C#利用SFTP实现上传下载由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.
sftp是ftp协议的升级版本,是牺牲上传速度为代价,换取安全性能,本人开始尝试使用Tamir.SharpSSH.dll但它对新版本的openssh 不支持,所有采用Ssh.Net方式 需要依赖:Renci.SshNet.dll 下载链接 。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
|
/// <summary>
/// SFTP操作类
/// </summary>
public
class
SFTPHelper
{
#region 字段或属性
private
SftpClient sftp;
/// <summary>
/// SFTP连接状态
/// </summary>
public
bool
Connected {
get
{
return
sftp.IsConnected; } }
#endregion
#region 构造
/// <summary>
/// 构造
/// </summary>
/// <param name="ip">IP</param>
/// <param name="port">端口</param>
/// <param name="user">用户名</param>
/// <param name="pwd">密码</param>
public
SFTPHelper(
string
ip,
string
port,
string
user,
string
pwd)
{
sftp =
new
SftpClient(ip, Int32.Parse(port), user, pwd);
}
#endregion
#region 连接SFTP
/// <summary>
/// 连接SFTP
/// </summary>
/// <returns>true成功</returns>
public
bool
Connect()
{
try
{
if
(!Connected)
{
sftp.Connect();
}
return
true
;
}
catch
(Exception ex)
{
// TxtLog.WriteTxt(CommonMethod.GetProgramName(), string.Format("连接SFTP失败,原因:{0}", ex.Message));
throw
new
Exception(
string
.Format(
"连接SFTP失败,原因:{0}"
, ex.Message));
}
}
#endregion
#region 断开SFTP
/// <summary>
/// 断开SFTP
/// </summary>
public
void
Disconnect()
{
try
{
if
(sftp !=
null
&& Connected)
{
sftp.Disconnect();
}
}
catch
(Exception ex)
{
// TxtLog.WriteTxt(CommonMethod.GetProgramName(), string.Format("断开SFTP失败,原因:{0}", ex.Message));
throw
new
Exception(
string
.Format(
"断开SFTP失败,原因:{0}"
, ex.Message));
}
}
#endregion
#region SFTP上传文件
/// <summary>
/// SFTP上传文件
/// </summary>
/// <param name="localPath">本地路径</param>
/// <param name="remotePath">远程路径</param>
public
void
Put(
string
localPath,
string
remotePath)
{
try
{
using
(var file = File.OpenRead(localPath))
{
Connect();
sftp.UploadFile(file, remotePath);
Disconnect();
}
}
catch
(Exception ex)
{
// TxtLog.WriteTxt(CommonMethod.GetProgramName(), string.Format("SFTP文件上传失败,原因:{0}", ex.Message));
throw
new
Exception(
string
.Format(
"SFTP文件上传失败,原因:{0}"
, ex.Message));
}
}
#endregion
#region SFTP获取文件
/// <summary>
/// SFTP获取文件
/// </summary>
/// <param name="remotePath">远程路径</param>
/// <param name="localPath">本地路径</param>
public
void
Get(
string
remotePath,
string
localPath)
{
try
{
Connect();
var byt = sftp.ReadAllBytes(remotePath);
Disconnect();
File.WriteAllBytes(localPath, byt);
}
catch
(Exception ex)
{
// TxtLog.WriteTxt(CommonMethod.GetProgramName(), string.Format("SFTP文件获取失败,原因:{0}", ex.Message));
throw
new
Exception(
string
.Format(
"SFTP文件获取失败,原因:{0}"
, ex.Message));
}
}
#endregion
#region 删除SFTP文件
/// <summary>
/// 删除SFTP文件
/// </summary>
/// <param name="remoteFile">远程路径</param>
public
void
Delete(
string
remoteFile)
{
try
{
Connect();
sftp.Delete(remoteFile);
Disconnect();
}
catch
(Exception ex)
{
// TxtLog.WriteTxt(CommonMethod.GetProgramName(), string.Format("SFTP文件删除失败,原因:{0}", ex.Message));
throw
new
Exception(
string
.Format(
"SFTP文件删除失败,原因:{0}"
, ex.Message));
}
}
#endregion
#region 获取SFTP文件列表
/// <summary>
/// 获取SFTP文件列表
/// </summary>
/// <param name="remotePath">远程目录</param>
/// <param name="fileSuffix">文件后缀</param>
/// <returns></returns>
public
ArrayList GetFileList(
string
remotePath,
string
fileSuffix)
{
try
{
Connect();
var files = sftp.ListDirectory(remotePath);
Disconnect();
var objList =
new
ArrayList();
foreach
(var file
in
files)
{
string
name = file.Name;
if
(name.Length > (fileSuffix.Length + 1) && fileSuffix == name.Substring(name.Length - fileSuffix.Length))
{
objList.Add(name);
}
}
return
objList;
}
catch
(Exception ex)
{
// TxtLog.WriteTxt(CommonMethod.GetProgramName(), string.Format("SFTP文件列表获取失败,原因:{0}", ex.Message));
throw
new
Exception(
string
.Format(
"SFTP文件列表获取失败,原因:{0}"
, ex.Message));
}
}
#endregion
#region 移动SFTP文件
/// <summary>
/// 移动SFTP文件
/// </summary>
/// <param name="oldRemotePath">旧远程路径</param>
/// <param name="newRemotePath">新远程路径</param>
public
void
Move(
string
oldRemotePath,
string
newRemotePath)
{
try
{
Connect();
sftp.RenameFile(oldRemotePath, newRemotePath);
Disconnect();
}
catch
(Exception ex)
{
// TxtLog.WriteTxt(CommonMethod.GetProgramName(), string.Format("SFTP文件移动失败,原因:{0}", ex.Message));
throw
new
Exception(
string
.Format(
"SFTP文件移动失败,原因:{0}"
, ex.Message));
}
}
#endregion
}
|
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我.
原文链接:http://www.cnblogs.com/cbread/p/6202069.html 。
最后此篇关于C#利用SFTP实现上传下载的文章就讲到这里了,如果你想了解更多关于C#利用SFTP实现上传下载的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。
如果您登录到 sftp 服务器, 用户名和密码是否安全发送? 或者您是否必须进行基于证书的身份验证以确保整个传输都是加密的? 如果这是依赖于客户端的,那么您知道 Tumbleweed 和 WinSCP
我们如何在不安装任何软件/工具的情况下验证服务器上是否已授予 SFTP 访问权限? 最佳答案 大多数服务器有 curl和 scp安装,您可以使用它登录到 SFTP 服务器。要使用 curl 测试您的凭
我需要为 SFTP 执行校验和(为了数据完整性)。我希望这可以在 SFTP 文件传输期间完成 - 我意识到这可能取决于产品(仅供引用:使用 CLEO VLTrader),但想知道这是否是惯例? 我也在
关闭。这个问题是off-topic .它目前不接受答案。 想改进这个问题? Update the question所以它是on-topic对于堆栈溢出。 8年前关闭。 Improve this que
我目前有一个 SFTP 脚本,它通过每天在 cron 上运行的 SSH 将最新版本的文件上传到服务器。但是我总是收到这样的电子邮件(我将服务器更改为 example.com fyi) /etc/cro
是否有使用“sftp”将远程服务器上的远程文件移动到另一个目录(在同一远程服务器上)的单行程序? 最佳答案 对于sftp,它是rename。如果你要求调用 sftp,使用 bash 就像是 sftp
嗨,我需要使用 NetSuite sFtp 功能从保存的搜索和 ftp'it 创建一个文件到另一台服务器。我想知道返回的数据后如何创建文件并将其传递给 FTP 对象?它将是一种csv形式,我可以将
我在 SFTP 位置创建了一个目录,我想将文件从一个 SFTP 目录移动到另一个目录,但是 cp那里不支持命令。 我怎样才能做到这一点? 最佳答案 核心 SFTP 协议(protocol)不支持复制远
我正在努力解决的问题有点复杂。我先向您解释设置环境。 我正在使用 PHPStorm 来处理 Symony2 项目。 我的 Apache 托管在通过“部署工具”连接到 PHPStorm 的 Debian
使用FTP,一切正常...但是.. 当我使用 SFTP 时,它成功连接,甚至当我使用“上传按钮”时,它成功上传文件...但是当我编辑+保存文件,它开始自动上传更改时,它无法上传(红色消息:文件....
我想将文件从远程服务器复制到不同目录中。例如,我想一次运行这4个命令。 scp remote:A/1.txt local:A/1.txt scp remote:A/2.txt local:A/2.tx
我可以使用 ssh/sftp(不带 -b 选项)登录我的 Remote sftp root@192.168.7.2 但是当我尝试 sftp -b commands.tmp root@192.168.7
我正在使用 Airflow v1.9rc5 中的 sftp_operator。我试图通过 SFTP 获取两个文件,但我得到: 错误 - 无法连接到主机:192.168.56.101,错误:没有可用的身
我想将 .png 文件从我计算机上的目录传输到远程服务器上的目录。 我必须使用 SFTP 来保护文件和传输模式。而且我已经有一个 UNIX 脚本 (.ksh) 文件以正常模式复制文件。如何在 SFTP
我正在使用 JSch 作为 SFTP 客户端,现在由于某些安全限制,我需要启用被动模式。 但我找不到启用被动模式的方法。 有人可以告诉我该怎么做吗? 最佳答案 “被动模式”是 FTP 协议(proto
我在 DigitalOcean 上有一个 Droplet,并创建了 SSH key 。我的 SSH 和 SFTP 访问与 Mac OSX 终端和传输完美配合。 但由于某种原因,SFTP 无法与 PHP
最近,我正在使用 SharpSSH 来处理远程文件。 SharpSSH 提供了用于删除远程目录的 rmdir API,但我发现当目录不为空时它会抛出异常并显示消息“Permission denied”
尝试通过 SFTP 连接到另一个集群中的文件,但没有任何效果。 Current spark version is : '2.2.0.2.6.4.0-91 . scala :2.11.8' . 下面是数
我已经使用 Package Installer (control+shift+p) 安装了 SFTP,但是现在当我点击“添加新服务器”时,无论我是哪个文件,它都只是添加了代码/json,我必须输入我的
我使用 DSL 配置和 spring。 我的路线如下: @Component public class UploadRoutesDefinition extends RouteBuilder {
我是一名优秀的程序员,十分优秀!