1 C
2 C++
3 Windows
4 Linux
5 数据库
- 5.1 SQL
- 5.2 Mysql
- 5.3 Oracle
- 5.5 Sqlite
- 5.6 数据库范式
- 5.7 游标
6 数据结构
7 算法
- 7.1 栈和队列
- 7.2 基本排序算法
- 7.3 合并排序
- 7.4 快速排序
- 7.5 优先级队列与堆排序
- 7.6 符号表及其基本实现
- 7.7 深度优先算法 DFS 和广度优先算法 BFS
- 7.8 桶排序
- 7.9 基数排序
8 Qt
9 AS400
10 Web
- 10.2 JavaScript
- 10.3 简述 cookie 和 session 及 token
- 10.4 Https 双向证书认证
- 10.5 URL 详解
12 C
13 框架
14 协议
15 工具
17 QA
文章来源于网络收集而来,版权归原创者所有,如有侵权请及时联系!
12.4 C井实现FTP下载文件
C# FTP下载文件
// FTP 下载
private int FtpFileDownload(string savePath, string fileName, string ftpServerIP, string ftpUserID, string ftpPassword)
{
string ftpServerPath = "ftp://" + ftpServerIP + ":23" + fileName; //":23"指定的端口,不填默认端口为21
string ftpUser = ftpUserID;
string ftpPwd = ftpPassword;
string saveFilePath = savePath;
FileStream outputStream = null;
FtpWebResponse response = null;
FtpWebRequest reqFTP;
try
{
outputStream = new FileStream(saveFilePath, FileMode.Create);
reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpServerPath));
reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;
reqFTP.UseBinary = true;
reqFTP.KeepAlive = false;
reqFTP.Timeout = 3000;
reqFTP.Credentials = new NetworkCredential(ftpUser, ftpPwd);
response = (FtpWebResponse)reqFTP.GetResponse();
Stream ftpStream = response.GetResponseStream();
int bufferSize = 2048;
int readCount;
ftpFileReadSize = 0;
byte[] buffer = new byte[bufferSize];
readCount = ftpStream.Read(buffer, 0, bufferSize);
while (readCount > 0)
{
ftpFileReadSize += readCount;
outputStream.Write(buffer, 0, readCount);
readCount = ftpStream.Read(buffer, 0, bufferSize);
}
ftpStream.Close();
outputStream.Close();
response.Close();
}
catch (Exception ex)
{
Trace.WriteLine("FtpClient异常:" + ex.Message);
if (outputStream != null)
{
outputStream.Close();
}
if (response != null)
{
response.Close();
}
return -1;
}
return 0;
}
// 获取FTP文件大小
private long GetFtpFileSize(string fileName, string ftpServerIP, string ftpUserID, string ftpPassword)
{
long fileSize = 0;
string ftpServerPath = "ftp://" + ftpServerIP + ":23" + fileName;
string ftpUser = ftpUserID;
string ftpPwd = ftpPassword;
FtpWebResponse response = null;
FtpWebRequest reqFTP;
try
{
reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpServerPath));
reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;
reqFTP.UseBinary = true;
reqFTP.KeepAlive = false;
reqFTP.Timeout = 3000;
reqFTP.Credentials = new NetworkCredential(ftpUser, ftpPwd);
reqFTP.Method = WebRequestMethods.Ftp.GetFileSize;//要求返回文件大小属性
response = (FtpWebResponse)reqFTP.GetResponse();
fileSize = response.ContentLength;
response.Close();
}
catch (Exception ex)
{
Trace.WriteLine("FtpClient异常:" + ex.Message);
if (response != null)
{
response.Close();
}
return -1;
}
return fileSize;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论