c#实现 ftp ;http;共享方式下载文件 并对比本地文件和服务器文件的更新时间 判断性下载
来源:互联网 发布:node v6.10.2 x64.msi 编辑:程序博客网 时间:2024/05/21 03:16
转载地址:http://www.cnblogs.com/Fengsp/archive/2012/08/06/2625149.htm
//从ftp服务器上下载文件的功能
public void Download(string ftpServerIP, string ftpUserID, string ftpPassword, string fileName, string Destination){
FtpWebRequest reqFTP;
try
{
FileStream outputStream = new FileStream(Destination + "\\" + fileName, FileMode.Create);
// 根据uri创建FtpWebRequest对象
reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + ftpServerIP + "/" + fileName));
// 指定执行什么命令
reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;
// 默认为true,连接不会被关闭
reqFTP.UseBinary = true;
// ftp用户名和密码
reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
FtpWebResponse ftpresponse = (FtpWebResponse)reqFTP.GetResponse();
Stream ftpStream = ftpresponse.GetResponseStream();
long cl = ftpresponse.ContentLength;
int bufferSize = 2048;
int readCount;
byte[] buffer = new byte[bufferSize];
readCount = ftpStream.Read(buffer, 0, bufferSize);
while (readCount > 0)
{
outputStream.Write(buffer, 0, readCount);
readCount = ftpStream.Read(buffer, 0, bufferSize);
}
ftpStream.Close();
outputStream.Close();
ftpresponse.Close();
}
catch (Exception ex)
{
}
}
//从http服务器上下载文件的功能
//为服务器路径server_path
//本地存储路径local_path
public void Download(string server_path, string local_path)
{
HttpWebRequest req;
try
{
string folder = local_path.Substring(0, local_path.LastIndexOf("\\"));
if (!Directory.Exists(folder))
Directory.CreateDirectory(folder);
// 根据uri创建FtpWebRequest对象
req = (HttpWebRequest)HttpWebRequest.Create(new Uri(server_path));
// 指定执行什么命令
req.Method = WebRequestMethods.Http.Get;
HttpWebResponse response = (HttpWebResponse)req.GetResponse();
if (File.Exists(local_path))
{
FileInfo localFile = new FileInfo(local_path);
if (localFile.LastWriteTime > response.LastModified) return;
}
FileStream outputStream = new FileStream(local_path, FileMode.Create);
Stream stream = response.GetResponseStream();
long cl = response.ContentLength;
int bufferSize = 2048;
int readCount;
byte[] buffer = new byte[bufferSize];
readCount = stream.Read(buffer, 0, bufferSize);
while (readCount > 0)
{
outputStream.Write(buffer, 0, readCount);
readCount = stream.Read(buffer, 0, bufferSize);
}
stream.Close();
outputStream.Close();
response.Close();
}
catch (Exception ex)
{
string s = ex.Message;
}
}
//从共享文件夹下载
public void Download(string server_path, string local_path, int i)
{
try
{
server_path = "\\" + server_path;
string folder = local_path.Substring(0, local_path.LastIndexOf("\\"));
if (!Directory.Exists(folder))
Directory.CreateDirectory(folder);
if (!File.Exists(server_path)) return;
//服务器文件信息
FileInfo serverInfo = new FileInfo(server_path);
DateTime serverTime = serverInfo.LastWriteTime;
if (File.Exists(local_path))
{
FileInfo localinfo = new FileInfo(local_path);
DateTime localTime = localinfo.LastWriteTime;
if (localTime >= serverTime)
return;
}
serverInfo.CopyTo(local_path, true);
return;
}
catch (Exception ex)
{
string s = ex.Message;
}
}
0 0
- c#实现 ftp ;http;共享方式下载文件 并对比本地文件和服务器文件的更新时间 判断性下载
- c#实现 ftp http共享方式下载文件 并对比本地文件和服务器文件的更新时间 判断性下载
- c#实现 ftp ;http;共享方式下载文件 并对比本地文件和服务器文件的更新时间 判断性下载
- 将本地文件上传到远程FTP服务器和从远程FTP服务器下载文件到本地
- 实现FTP服务器文件下载
- FTP方式的文件上传和下载
- ftp实现文件的上传和下载
- c#实现http文件下载
- C# ftp 下载文件
- C# 下载FTP服务器一个文件夹内的所有文件
- JAVA中的FtpClient与FTPClient,并实现jsp页面下载ftp服务器上的文件
- Java 使用ftp的方式上传文件到linux,并实现删除,下载
- JAVA实现FTP服务器文件的上传,下载,删除功能
- Android 访问FTP 服务器,实现文件的下载
- FTP服务器文件的上传,下载和获取
- wget下载ftp文件,通过用户名和密码的方式
- Http服务器下载文件
- 用 PS 下载 FTP 服务器的文件
- 当方法的返回类型是数组或集合时,不要返回null,尽量返回容量为0的数组或集合
- 第六章
- Bootstrap输入框组
- django1.8的版本中的应用列表中加入重新定向会出错
- 融云语音通话弹出多个窗口的问题
- c#实现 ftp ;http;共享方式下载文件 并对比本地文件和服务器文件的更新时间 判断性下载
- 【虫师--系列19】软件性能测试的本质
- 脑袋决定口袋,思维决定未来!
- java24.线程------实现
- Android 高仿 频道管理----网易、今日头条、腾讯视频 (可以拖动的GridView)附源码DEMO
- ROS:ubuntu-Ros使用OrbSLAM
- Spring学习总结(5)——IOC注入方式总结
- 前端面试题精选系列汇总
- MFC中热键操作