- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我正在尝试使用 FtpWebRequest
下载文件。
private void DownloadFile(string userName, string password, string ftpSourceFilePath, string localDestinationFilePath)
{
int bytesRead = 0;
byte[] buffer = new byte[1024];
FtpWebRequest request = CreateFtpWebRequest(ftpSourceFilePath, userName, password, true);
request.Method = WebRequestMethods.Ftp.DownloadFile;
Stream reader = request.GetResponse().GetResponseStream();
BinaryWriter writer = new BinaryWriter(File.Open(localDestinationFilePath, FileMode.CreateNew));
while (true)
{
bytesRead = reader.Read(buffer, 0, buffer.Length);
if (bytesRead == 0)
break;
writer.Write(buffer, 0, bytesRead);
}
}
它使用我创建的这个 CreateFtpWebRequest
方法:
private FtpWebRequest CreateFtpWebRequest(string ftpDirectoryPath, string userName, string password, bool keepAlive = false)
{
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(new Uri(ftpDirectoryPath));
//Set proxy to null. Under current configuration if this option is not set then the proxy that is used will get an html response from the web content gateway (firewall monitoring system)
request.Proxy = null;
request.UsePassive = true;
request.UseBinary = true;
request.KeepAlive = keepAlive;
request.Credentials = new NetworkCredential(userName, password);
return request;
}
它下载它。但是信息总是损坏的。有人知道这是怎么回事吗?
最佳答案
刚想通了:
private void DownloadFile(string userName, string password, string ftpSourceFilePath, string localDestinationFilePath)
{
int bytesRead = 0;
byte[] buffer = new byte[2048];
FtpWebRequest request = CreateFtpWebRequest(ftpSourceFilePath, userName, password, true);
request.Method = WebRequestMethods.Ftp.DownloadFile;
Stream reader = request.GetResponse().GetResponseStream();
FileStream fileStream = new FileStream(localDestinationFilePath, FileMode.Create);
while (true)
{
bytesRead = reader.Read(buffer, 0, buffer.Length);
if (bytesRead == 0)
break;
fileStream.Write(buffer, 0, bytesRead);
}
fileStream.Close();
}
不得不改用 FileStream。
关于c# - 使用 FtpWebRequest 下载文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12519290/
我正在寻找一小段示例代码,该示例代码使用System.Net.FtpWebRequest命名空间来获取ftp服务器上指定的远程文件的时间戳。我知道我需要将请求对象的Method属性设置为WebRequ
我在这里使用示例: http://msdn.microsoft.com/en-us/library/ms229715.aspx将 1GB 的大文件上传到 FTP 服务器。然而它就在线窒息: byte[
我制作了一个批处理类来检查 FTP 上的文件、下载它们并在 FTP 上删除它们。 当我手动运行它(不是批量运行)时,它运行完美,下载 FTP 中的所有文件并在下载完成后删除它们。 当我尝试批量运行时,
我在使用 C# 中的 FtpWebRequest 类时遇到了问题。 当我第一次尝试使用正确的凭据将文件上传到 FTP 服务器,然后第二次使用错误的凭据(用户名相同但密码错误)时,没有抛出异常并且文件仍
我想连接到工作在“主动模式”下的 FTP 服务器。这意味着,客户端可以发送应该在其上进行数据连接的端口号。通常这是一个随机端口。 (N > 1023) 在我们的例子中,如果我们总是可以使用特定端口连接
我正在使用以下代码从远程 ftp 服务器下载文件: FtpWebRequest request = (FtpWebRequest)WebRequest.Create(serverPat
我正在为 .NET 创建一个通用的 FTP 类。我似乎让事情运作良好,但有一些细节我有点不确定。 MSDN 上的文档完全没有提供有关这些细节的信息。 例如,在执行WebRequestMethods.F
正在尝试将文件上传到 FTP。登录时我设置在目录/out,上传前需要返回一个目录然后上到目录/in。我有这段代码: FtpWebRequest req = (FtpWebRequest)
我想序列化 FtpWebRequest 类的一个实例。是否可以?当我尝试时,它给了我错误,类未标记为可序列化,这意味着我无法序列化该对象,但在 msdn 文档中,他们提到了 ISerializable
我正在开发一个将多个文件上传到 FTP 的程序。为此我需要完成 8 个 Action : 在 FTP 中创建一个新文件夹 上传三个文件到新目录 在新目录下创建三个子目录 根据我收集到的信息,我只能处理
我正在组装一个简单的应用程序,我让它使用 FtpWebRequest 上传、下载、删除文件。但是我找不到如何使用 FtpWebRequest 移动文件。在不使用另一个外部依赖项的情况下将文件从一个目录
我正在尝试创建一个简单的方法,使用 FtpWebRequest 和方法 WebRequestMethods.Ftp.DownloadFile 从 FTP 下载文件。问题是我不想显示下载进度,因此需要提
以下代码旨在通过 FTP 检索文件。但是,我遇到了错误。 serverPath = "ftp://x.x.x.x/tmp/myfile.txt"; FtpWebRequest request = (F
我有以下代码来通过 FTP 检索文件: try { FtpWebRequest request = (FtpWebRequest)WebRequ
我最近使用 C# 将 Windows cmd 转换为 FtpWebRequest,以将文件上传到 IBM 大型机数据集。 Look here。 运行两者时,我发现它们上传到不同的数据集,而它们应该完全
使用FtpWebRequest列出目录的内容;但是,它没有显示隐藏文件。 如何让它显示隐藏文件? FtpWebRequest request = (FtpWebRequest)WebRequest.C
我需要列出 FTP 服务器上的数千个文件,并删除必要的文件。正如您所猜测的,性能至关重要,因此我需要一种重用 FTP 连接的方法。 MSDN上没有足够的关于FtpWebRequests连接使用的解释!
我正在尝试从包含大约 9000 个文件的 FTP 位置检索文件列表。 但是下面的代码总是只给出97个文件。在第 98 个文件的循环开始时,StreamReader.Peek() 变为 -1 输出“te
我一直致力于为客户构建一项功能,使他们能够从 www.site1.com 上传图像,将图像 URL 存储到数据库并将图像存储到 www.site2.com/images 上的文件中.我已经设法将文件上
我在使用 .NET FTPWebRequest 类将文件上传到 vsftpd 服务器时遇到了一些问题。 首先,有没有办法使用 ListDirectoryDetails 请求列出隐藏文件?现在我没有
我是一名优秀的程序员,十分优秀!