FtpWebRequest上传、下载文件
2008-11-11 12:47
453 查看
接到一个任务,该任务需要从数据支持部的FTP上下载4个文件。
数据支持部的同事给了4个工具分别用下来载这4个文件
因为每天要定时下载,所以加入了计划任务。
这样一来,本可以做为一个任务来完成的东东却硬被分成了4个任务,
这还不是大问题,最大的问题是如果要下载的文件名规则有了变化,我就要分别改这4个工具的配置文件。
这4个工具虽能满足要求,但有点烦琐。看来还是不能偷懒了 - -
自己用FtpWebRequest来实现吧
下载文件
view plaincopy to clipboardprint?
/// <summary>
/// 下载文件
/// </summary>
/// <param name="filename"></param>
private static void DownLoadFile(string filename)
{
FtpWebRequest req = (FtpWebRequest)WebRequest.Create("ftp://" + FtpAddress + "/" + filename);
req.Method = WebRequestMethods.Ftp.DownloadFile;
req.UseBinary = true;
req.UsePassive = true;
req.Credentials = new NetworkCredential(FtpUid, FtpPwd);
try
{
using (FtpWebResponse res = (FtpWebResponse)req.GetResponse())
{
string localfile = Path.Combine(LocalPath, filename);
FileStream fs = new FileStream(localfile, FileMode.Create, FileAccess.Write);
int buffer = 1024; //1K缓冲
byte[] b = new byte[buffer];
int i = 0;
Stream stream = res.GetResponseStream();
while ((i = stream.Read(b, 0, buffer)) > 0)
{
fs.Write(b, 0, i);
}
}
Console.WriteLine(filename + " download!");
Log(filename + "下载成功");
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
Log(ex.ToString());
}
finally
{
}
}
dp.SyntaxHighlighter.ClipboardSwf = 'SyntaxHighlighter/Scripts/clipboard.swf';
dp.SyntaxHighlighter.HighlightAll('70929fdd60e24c25904f18772b7ebab3');
获取文件列表
view plaincopy to clipboardprint?
/// <summary>
/// 获取FTP文件列表
/// </summary>
/// <returns></returns>
private static List<String> GetFileList()
{
List<string> list = new List<string>();
FtpWebRequest req = (FtpWebRequest)WebRequest.Create(new Uri("ftp://" + FtpAddress + FtpRemotePath));
req.Credentials = new NetworkCredential(FtpUid, FtpPwd);
req.Method = WebRequestMethods.Ftp.ListDirectory;
req.UseBinary = true;
req.UsePassive = true;
try
{
using (FtpWebResponse res = (FtpWebResponse)req.GetResponse())
{
using (StreamReader sr = new StreamReader(res.GetResponseStream()))
{
string s;
while ((s = sr.ReadLine()) != null)
{
list.Add(s);
}
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
Log("下载文件列表失败:");
Log(ex.ToString());
}
return list;
}
dp.SyntaxHighlighter.ClipboardSwf = 'SyntaxHighlighter/Scripts/clipboard.swf';
dp.SyntaxHighlighter.HighlightAll('42d5c3acd10f42a3859050b686e74059');
上传文件
view plaincopy to clipboardprint?
private static void UploadFile(string localFile)
{
FileInfo fi = new FileInfo(localFile);
FileStream fs = fi.OpenRead();
long length = fs.Length;
FtpWebRequest req = (FtpWebRequest)WebRequest.Create("ftp://" + FtpAddress + FtpRemotePath + fi.Name);
req.Credentials = new NetworkCredential(FtpUid, FtpPwd);
req.Method = WebRequestMethods.Ftp.UploadFile;
req.UseBinary = true;
req.ContentLength = length;
req.Timeout = 10 * 1000;
try
{
Stream stream = req.GetRequestStream();
int BufferLength = 2048; //2K
byte[] b = new byte[BufferLength];
int i;
while ((i = fs.Read(b, 0, BufferLength)) > 0)
{
stream.Write(b, 0, i);
}
stream.Close();
stream.Dispose();
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
数据支持部的同事给了4个工具分别用下来载这4个文件
因为每天要定时下载,所以加入了计划任务。
这样一来,本可以做为一个任务来完成的东东却硬被分成了4个任务,
这还不是大问题,最大的问题是如果要下载的文件名规则有了变化,我就要分别改这4个工具的配置文件。
这4个工具虽能满足要求,但有点烦琐。看来还是不能偷懒了 - -
自己用FtpWebRequest来实现吧
下载文件
view plaincopy to clipboardprint?
/// <summary>
/// 下载文件
/// </summary>
/// <param name="filename"></param>
private static void DownLoadFile(string filename)
{
FtpWebRequest req = (FtpWebRequest)WebRequest.Create("ftp://" + FtpAddress + "/" + filename);
req.Method = WebRequestMethods.Ftp.DownloadFile;
req.UseBinary = true;
req.UsePassive = true;
req.Credentials = new NetworkCredential(FtpUid, FtpPwd);
try
{
using (FtpWebResponse res = (FtpWebResponse)req.GetResponse())
{
string localfile = Path.Combine(LocalPath, filename);
FileStream fs = new FileStream(localfile, FileMode.Create, FileAccess.Write);
int buffer = 1024; //1K缓冲
byte[] b = new byte[buffer];
int i = 0;
Stream stream = res.GetResponseStream();
while ((i = stream.Read(b, 0, buffer)) > 0)
{
fs.Write(b, 0, i);
}
}
Console.WriteLine(filename + " download!");
Log(filename + "下载成功");
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
Log(ex.ToString());
}
finally
{
}
}
/// <summary> /// 下载文件 /// </summary> /// <param name="filename"></param> private static void DownLoadFile(string filename) { FtpWebRequest req = (FtpWebRequest)WebRequest.Create("ftp://" + FtpAddress + "/" + filename); req.Method = WebRequestMethods.Ftp.DownloadFile; req.UseBinary = true; req.UsePassive = true; req.Credentials = new NetworkCredential(FtpUid, FtpPwd); try { using (FtpWebResponse res = (FtpWebResponse)req.GetResponse()) { string localfile = Path.Combine(LocalPath, filename); FileStream fs = new FileStream(localfile, FileMode.Create, FileAccess.Write); int buffer = 1024; //1K缓冲 byte[] b = new byte[buffer]; int i = 0; Stream stream = res.GetResponseStream(); while ((i = stream.Read(b, 0, buffer)) > 0) { fs.Write(b, 0, i); } } Console.WriteLine(filename + " download!"); Log(filename + "下载成功"); } catch (Exception ex) { Console.WriteLine(ex.ToString()); Log(ex.ToString()); } finally { } }
dp.SyntaxHighlighter.ClipboardSwf = 'SyntaxHighlighter/Scripts/clipboard.swf';
dp.SyntaxHighlighter.HighlightAll('70929fdd60e24c25904f18772b7ebab3');
获取文件列表
view plaincopy to clipboardprint?
/// <summary>
/// 获取FTP文件列表
/// </summary>
/// <returns></returns>
private static List<String> GetFileList()
{
List<string> list = new List<string>();
FtpWebRequest req = (FtpWebRequest)WebRequest.Create(new Uri("ftp://" + FtpAddress + FtpRemotePath));
req.Credentials = new NetworkCredential(FtpUid, FtpPwd);
req.Method = WebRequestMethods.Ftp.ListDirectory;
req.UseBinary = true;
req.UsePassive = true;
try
{
using (FtpWebResponse res = (FtpWebResponse)req.GetResponse())
{
using (StreamReader sr = new StreamReader(res.GetResponseStream()))
{
string s;
while ((s = sr.ReadLine()) != null)
{
list.Add(s);
}
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
Log("下载文件列表失败:");
Log(ex.ToString());
}
return list;
}
/// <summary> /// 获取FTP文件列表 /// </summary> /// <returns></returns> private static List<String> GetFileList() { List<string> list = new List<string>(); FtpWebRequest req = (FtpWebRequest)WebRequest.Create(new Uri("ftp://" + FtpAddress + FtpRemotePath)); req.Credentials = new NetworkCredential(FtpUid, FtpPwd); req.Method = WebRequestMethods.Ftp.ListDirectory; req.UseBinary = true; req.UsePassive = true; try { using (FtpWebResponse res = (FtpWebResponse)req.GetResponse()) { using (StreamReader sr = new StreamReader(res.GetResponseStream())) { string s; while ((s = sr.ReadLine()) != null) { list.Add(s); } } } } catch (Exception ex) { Console.WriteLine(ex.ToString()); Log("下载文件列表失败:"); Log(ex.ToString()); } return list; }
dp.SyntaxHighlighter.ClipboardSwf = 'SyntaxHighlighter/Scripts/clipboard.swf';
dp.SyntaxHighlighter.HighlightAll('42d5c3acd10f42a3859050b686e74059');
上传文件
view plaincopy to clipboardprint?
private static void UploadFile(string localFile)
{
FileInfo fi = new FileInfo(localFile);
FileStream fs = fi.OpenRead();
long length = fs.Length;
FtpWebRequest req = (FtpWebRequest)WebRequest.Create("ftp://" + FtpAddress + FtpRemotePath + fi.Name);
req.Credentials = new NetworkCredential(FtpUid, FtpPwd);
req.Method = WebRequestMethods.Ftp.UploadFile;
req.UseBinary = true;
req.ContentLength = length;
req.Timeout = 10 * 1000;
try
{
Stream stream = req.GetRequestStream();
int BufferLength = 2048; //2K
byte[] b = new byte[BufferLength];
int i;
while ((i = fs.Read(b, 0, BufferLength)) > 0)
{
stream.Write(b, 0, i);
}
stream.Close();
stream.Dispose();
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
相关文章推荐
- 使用.net FtpWebRequest 实现FTP常用功能 上传 下载 获取文件列表 移动 切换目录 改名
- FtpWebRequest(转)---关于C/S中文件的上传,下载,获得文件列表
- 使用FtpWebRequest 类操作(上传、下载和删除)FTP上的XML文件
- 使用.net FtpWebRequest 实现FTP常用功能 上传 下载 获取文件列表 移动 切换目录 改名 .
- FtpWebRequest FTP异步下载、异步上传文件
- FtpWebRequest 上传文件
- FTP FtpWebRequest 异步上传文件
- C# HttpWebRequest可断点上传,下载文件;SOCKET多线程多管道可断点传送大文件
- 在Web/Phpstorm中设置连接FTP(附带:文件比较,上传下载,同步等)
- asp.net使用FtpWebRequest向ftp上传文件报错 基础连接已经关闭: 服务器提交了协议冲突
- 【12】FtpWebRequest上传下载
- Java Web 开发中文件的上传与下载(FTP/HTTP)
- C# WebClient进行FTP服务上传文件和下载文件
- c# 使用FtpWebRequest,FtpWebResponse ,Stream 实现获取文件列表及下载
- HttpWebRequest 下载网页Html代码 下载文件(Remote和FTP)Get方式
- 【12】FtpWebRequest上传下载
- 通过FTP服务端来实现匿名用户和基本用户的访问,可以实现文件上传和下载。通过web网站来浏览内容
- FTP(FtpWebRequest/FtpWebResponse) 上传下载
- javaweb 文件上传(fileupload) 下载
- java实现ftp文件的上传与下载