以下代码旨在通过FTP检索文件.但是,我收到了一个错误.
serverPath = "ftp://x.x.x.x/tmp/myfile.txt";
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(serverPath);
request.KeepAlive = true;
request.UsePassive = true;
request.UseBinary = true;
request.Method = WebRequestMethods.Ftp.DownloadFile;
request.Credentials = new NetworkCredential(username, password);
// Read the file from the server & write to destination
using (FtpWebResponse response = (FtpWebResponse)request.GetResponse()) // Error here
using (Stream responseStream = response.GetResponseStream())
using (StreamReader reader = new StreamReader(responseStream))
using (StreamWriter destination = new StreamWriter(destinationFile))
{
destination.Write(reader.ReadToEnd());
destination.Flush();
}
Run Code Online (Sandbox Code Playgroud)
错误是:
远程服务器返回错误:(550)文件不可用(例如,找不到文件,没有访问权限)
该文件肯定存在于远程计算机上,我可以手动执行此ftp(即我有权限).任何人都可以告诉我为什么我可能会收到此错误?
这有点棘手.
我正在异步上传文件到FTP.上传每个文件后,我正在检查该文件的上传操作的状态.这可以使用该请求的FtpWebResponse对象的StatusCode属性来完成.代码段如下所示.
FileStream fs = File.Open(fileName, FileMode.Open);
while ((iWork = fs.Read(buf, 0, buf.Length)) > 0)
requestStream.Write(buf, 0, iWork);
requestStream.Close();
FtpWebResponse wrRet = ((FtpWebResponse)state.Request.GetResponse());
Run Code Online (Sandbox Code Playgroud)
根据msdn,大约有37个StatusCode值.我不知道这些状态代码值中的哪一个将确保文件成功上传.我在代码中用来检查成功的其中一些是:
wrRet.StatusCode == FtpStatusCode.CommandOK
wrRet.StatusCode == FtpStatusCode.ClosingData
wrRet.StatusCode == FtpStatusCode.ClosingControl
wrRet.StatusCode == FtpStatusCode.ConnectionClosed
wrRet.StatusCode == FtpStatusCode.FileActionOK
wrRet.StatusCode == FtpStatusCode.FileStatus
Run Code Online (Sandbox Code Playgroud)
但我不知道其余的.我需要确定这些代码,因为基于上传操作的失败或成功,我还要执行其他相关操作.错误的条件会影响剩余的代码.我想到的另一个想法是简单地将上面的代码放入try..catch而不依赖于这些状态代码.有了这个,我不会依赖于状态代码,并假设任何失败将始终指向catch块.如果这是正确的方法,请告诉我.
我正在编写一个使用带有凭据的ftp服务器的程序.我正在尝试从服务器检索目录列表但是当我到达该行时:
string line = reader.ReadLine();
Run Code Online (Sandbox Code Playgroud)
我得到的字符串只包含:"无法打开"主机:/ lib1 \"."
如果我尝试获取另一行,则抛出下一个异常:远程服务器返回错误:(550)文件不可用(例如,找不到文件,没有访问权限).
我肯定(使用另一个ftp应用程序)知道ftp服务器上存在'lib1'目录并且我的凭据(用户名和密码)是正确的.
这是我的代码:
public class FTPClient
{
public string UserName { get; set; }
public string Password { get; set; }
public string IpAddress { get; set; }
public int Port { get; set; }
public FTPClient(string _userName, string _password, string _address, int _port)
{
UserName = _userName;
Password = _password;
IpAddress = _address;
Port = _port;
}
public void GetDirectoriesList(string _path)
{
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(new Uri("ftp://" +
IpAddress + _path)); …Run Code Online (Sandbox Code Playgroud) 我想从ftp服务器下载基于日期时间的文件..我可以从CuteFtp第三方访问这个Ftp而且每件事都是Okey ..但是当我在第一行运行下面的代码时GetRespone()我得到这个错误:操作有时间出.我用webclient requet以编程方式从这个FTP下载了一个示例文件,它很好..但是我需要使用FtpWebRequest获取listDirectoryDetail而webClient不支持..还有一件事,请求中有一个异常:FtpWebRequest.ContentType抛出异常类型System.NotSupportedException.
这是我的代码:
Uri uri = new Uri("ftp://192.168.1.5:2100/");//the private address
if (uri.Scheme != Uri.UriSchemeFtp)
{
return;
}
FtpWebRequest reqFTP;
reqFTP = (FtpWebRequest)WebRequest.Create(uri);
reqFTP.Credentials = new NetworkCredential("myuser", "mypass");
reqFTP.KeepAlive = false;
reqFTP.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
reqFTP.UseBinary = true;
reqFTP.Proxy = null;
reqFTP.UsePassive = false;
FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
Run Code Online (Sandbox Code Playgroud)
请帮忙 :(
第一次海报,长期读者.我有一个非常烦人的问题,让我感到紧张.我已经设置了一个程序,所以我在FTP服务器上监听新文件,如果我下载了一个新文件.从那里我处理文件中的一些信息,等等.当我第二次运行我的序列时,我的问题出现了.也就是说,在我下载的第一个文件中,一切都很好,但是一旦检测到新文件并且我的程序尝试下载它,我的程序就会挂起.
private static void DownloadFile(string s)
{
try
{
FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://blabla.com/"+s);
request.Method = WebRequestMethods.Ftp.DownloadFile;
request.Credentials = new NetworkCredential("xxx" ,"zzz");
using (FtpWebResponse partResponse = (FtpWebResponse)request.GetResponse())
{
Stream partReader = partResponse.GetResponseStream();
byte[] buffer = new byte[1024];
FileInfo fi = new FileInfo(path);
FileStream memStream = fi.Create();
while (true)
{
int bytesRead = partReader.Read(buffer, 0, buffer.Length - 1);
if (bytesRead == 0)
break;
memStream.Write(buffer, 0, bytesRead);
}
partResponse.Close();
memStream.Close();
}
Console.WriteLine(DateTime.Now + " file downloaded");
MoveFileToInProgress(s);
}
catch (Exception e)
{
Console.WriteLine(e.Message); …Run Code Online (Sandbox Code Playgroud) FtpWebResponse实现了IDisposable,但它没有Dispose方法.怎么可能?
我们需要使用vb.net从远程FTP服务器获取大约100个非常小的文件.我们公司不会让我们购买(或安装)任何第三方ftp库...因此我们被迫使用类似FtpWebRequest的东西.(或者是否有更好的免费,选择已经是Visual Studio的一部分?)
这种方法有效,但速度非常慢.(我假设因为不断登录/退出.)
Log in with user name and password. Get a file-list from the remote server. Log out Use that file-list to get each file separtely: Log in, get the file, log out. Log in 99 more times, get each file, log out each time.
相反,我们可能应该这样做,但它永远不会起作用:
Log in with user name and password. ONCE. Get a list of filenames. Download each file. Log out ONCE.
我们在网上找到了"获取FTP文件列表"以及后来"如何用FTP下载1个文件"的在线例子......但我们从未看到"获取每个文件名,现在就下载".
Dim fwr As Net.FtpWebRequest = Net.FtpWebRequest.Create(ftpSite) fwr.Credentials = New NetworkCredential(userName, password) fwr.KeepAlive = …
我正在尝试登录到 ftp 服务器。在 C# 中使用以下代码。
FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://ftp-server");
request.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
// This example assumes the FTP site uses anonymous logon.
//request.AuthenticationLevel = System.Net.Security.AuthenticationLevel.MutualAuthRequired;
request.Credentials = new NetworkCredential("userName", "password!#£");
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
Run Code Online (Sandbox Code Playgroud)
但是,当密码包含一些特殊字符时,登录会失败。例如('!' 或 '£')?我得到以下异常。
Unhandled Exception: System.Net.WebException: The remote server returned an error: (530) Not logged in.
at System.Net.FtpWebRequest.SyncRequestCallback(Object obj)
at System.Net.FtpWebRequest.RequestCallback(Object obj)
at System.Net.CommandStream.Dispose(Boolean disposing)
at System.IO.Stream.Close()
at System.IO.Stream.Dispose()
at System.Net.ConnectionPool.Destroy(PooledStream pooledStream)
at System.Net.ConnectionPool.PutConnection(PooledStream pooledStream, Object owningObject, Int32 creationTimeout, Bo
ean canReuse)
at System.Net.FtpWebRequest.FinishRequestStage(RequestStage stage)
at …Run Code Online (Sandbox Code Playgroud) 我正在用C#构建一个FTP实用程序类.如果在WebException调用时抛出a FtpWebRequest.GetResponse(),在我的情况下,对于远程服务器上不存在的请求文件抛出异常,该FtpWebResponse变量超出范围.
但即使我在try..catch块外声明变量,我也会得到一个编译错误,说"使用未分配的局部变量'响应'",但据我所知,除非通过该FtpWebRequest.GetResponse()方法分配响应,否则无法分配它.
有人可以建议,还是我错过了一些明显的东西?
谢谢!
这是我目前的方法:
private void Download(string ftpServer, string ftpPath, string ftpFileName, string localPath,
string localFileName, string ftpUserID, string ftpPassword)
{
FtpWebRequest reqFTP;
FtpWebResponse response;
try
{
reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://"
+ ftpServer + "/" + ftpPath + "/" + ftpFileName));
reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;
reqFTP.UseBinary = true;
reqFTP.Credentials = new NetworkCredential(ftpUserID,
ftpPassword);
/* HERE IS WHERE THE EXCEPTION IS THROWN FOR FILE NOT AVAILABLE*/
response = (FtpWebResponse)reqFTP.GetResponse();
Stream ftpStream …Run Code Online (Sandbox Code Playgroud) 我有一个应用程序执行以下操作:目录列表,下载文件,全部下载.
我从WebRequestMethods.Ftp.ListDirectoryDetails获取文件名时遇到问题.对于每种情况,似乎都不可能这样做.
WebRequestMethods.Ftp.ListDirectoryDetails以下列方式返回lineItem:
"-rw-r - r-- 1 ftp ftp 39979 Aug 01 16:02 db to pc 2014-08-05 07-30-00.csv"
我使用第一个字符来确定它是文件还是目录.然后我在空间上拆分文件,并在拆分中的固定索引量之后获取文件名.我的实现中的问题是,如果一个文件有多个空格,那么它将被错误地引用,空格较少,并且在尝试下载时不会找到该文件.
我无法使用split.last(),因为文件名可以包含空格,也不能包含WebRequestMethods.Ftp.ListDirectory,因为它不允许我们区分目录和没有扩展名的文件.也不是正则表达式,因为文件名可以包含日期.寻找完全涵盖所有案例的解决方案的任何帮助都会很棒.
bool isDirectory = line.Substring(0,1).Equals("d", System.StringComparison.OrdinalIgnoreCase);
string[] itemNames = line.Split(new[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries).Select((value, index) => new { value, index }).Where(i => i.index > 7).Select(i => i.value).ToArray();
string val = string.Join(" ", itemNames);
Run Code Online (Sandbox Code Playgroud) 我正在使用以下代码从远程ftp服务器下载文件:
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(serverPath);
request.KeepAlive = true;
request.UsePassive = true;
request.UseBinary = true;
request.Method = WebRequestMethods.Ftp.DownloadFile;
request.Credentials = new NetworkCredential(userName, password);
using (FtpWebResponse response = (FtpWebResponse)request.GetResponse())
using (Stream responseStream = response.GetResponseStream())
using (StreamReader reader = new StreamReader(responseStream))
using (StreamWriter destination = new StreamWriter(destinationFile))
{
destination.Write(reader.ReadToEnd());
destination.Flush();
}
Run Code Online (Sandbox Code Playgroud)
我正在下载的文件是一个DLL,我的问题是它以某种方式被这个过程改变了.我知道这是因为文件大小正在增加.我怀疑这部分代码是错误的:
destination.Write(reader.ReadToEnd());
destination.Flush();
Run Code Online (Sandbox Code Playgroud)
任何人都可以提出任何可能出错的想法吗?
是否可以在ftp上写一个txt文件(不要将文件上传到ftp!)直接在ftp服务器上写一个txt文件)并从ftp上的文件中读取(不从ftp下载文件!)直接从txt读取ftp服务器上的文件?我搜索了但发现上传了一个文件,并用FtpWebRequest类下载了一个文件.注意:FTP服务器使用凭据.