以下代码旨在通过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();
}
错误是:
远程服务器返回错误:(550)文件不可用(例如,找不到文件,没有访问权限)
该文件肯定存在于远程计算机上,我可以手动执行此ftp(即我有权限)。谁能告诉我为什么我会收到这个错误?
答案 0 :(得分:48)
我知道这是一个旧帖子,但我在这里添加以供将来参考。这是我找到的解决方案:
private void DownloadFileFTP()
{
string inputfilepath = @"C:\Temp\FileName.exe";
string ftphost = "xxx.xx.x.xxx";
string ftpfilepath = "/Updater/Dir1/FileName.exe";
string ftpfullpath = "ftp://" + ftphost + ftpfilepath;
using (WebClient request = new WebClient())
{
request.Credentials = new NetworkCredential("UserName", "P@55w0rd");
byte[] fileData = request.DownloadData(ftpfullpath);
using (FileStream file = File.Create(inputfilepath))
{
file.Write(fileData, 0, fileData.Length);
file.Close();
}
MessageBox.Show("Download Complete");
}
}
根据Ilya Kogan的优秀建议更新
答案 1 :(得分:23)
FptWebRequest class reference中的这一段可能是您感兴趣的:
URI可以是相对的或绝对的。 如果URI是表单 “ftp://contoso.com/%2fpath”(%2f是 一个转义'/'),那么URI就是 绝对的,当前目录是 /路径。但是,如果URI是 首先形成“ftp://contoso.com/path” .NET Framework登录到FTP 服务器(使用用户名和 密码由凭证设置 property),然后是当前目录 设置为/ path。
答案 2 :(得分:15)
使用.NET框架从FTP服务器下载二进制文件的最简单方法是使用WebClient.DownloadFile
:
WebClient client = new WebClient();
client.Credentials = new NetworkCredential("username", "password");
client.DownloadFile(
"ftp://ftp.example.com/remote/path/file.zip", @"C:\local\path\file.zip");
仅当您需要更高的控制权时才使用FtpWebRequest
WebClient
不提供(例如TLS / SSL加密,进度监控等)。简单的方法是使用Stream.CopyTo
FileStream
FtpWebRequest request =
(FtpWebRequest)WebRequest.Create("ftp://ftp.example.com/remote/path/file.zip");
request.Credentials = new NetworkCredential("username", "password");
request.Method = WebRequestMethods.Ftp.DownloadFile;
using (Stream ftpStream = request.GetResponse().GetResponseStream())
using (Stream fileStream = File.Create(@"C:\local\path\file.zip"))
{
ftpStream.CopyTo(fileStream);
}
如果您需要监控下载进度,则必须自行复制内容:
FtpWebRequest request =
(FtpWebRequest)WebRequest.Create("ftp://ftp.example.com/remote/path/file.zip");
request.Credentials = new NetworkCredential("username", "password");
request.Method = WebRequestMethods.Ftp.DownloadFile;
using (Stream ftpStream = request.GetResponse().GetResponseStream())
using (Stream fileStream = File.Create(@"C:\local\path\file.zip"))
{
byte[] buffer = new byte[10240];
int read;
while ((read = ftpStream.Read(buffer, 0, buffer.Length)) > 0)
{
fileStream.Write(buffer, 0, read);
Console.WriteLine("Downloaded {0} bytes", fileStream.Position);
}
}
对于GUI进度(WinForms ProgressBar
),请参阅:
FtpWebRequest FTP download with ProgressBar
如果要从远程文件夹下载所有文件,请参阅
C# Download all files and subdirectories through FTP
答案 3 :(得分:2)
我有同样的问题!
我的解决方案是将public_html
文件夹插入下载网址。
服务器上的真实文件位置:
myhost.com/public_html/myimages/image.png
网址:
www.myhost.com/myimages/image.png
答案 4 :(得分:2)
css
我希望它可以帮到你。
答案 5 :(得分:0)
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(serverPath);
在此之后,您可以使用以下行来避免错误..(拒绝访问等)
request.Proxy = null;
答案 6 :(得分:0)
public void download(string remoteFile, string localFile)
{
private string host = "yourhost";
private string user = "username";
private string pass = "passwd";
private FtpWebRequest ftpRequest = null;
private FtpWebResponse ftpResponse = null;
private Stream ftpStream = null;
private int bufferSize = 2048;
try
{
ftpRequest = (FtpWebRequest)FtpWebRequest.Create(host + "/" + remoteFile);
ftpRequest.Credentials = new NetworkCredential(user, pass);
ftpRequest.UseBinary = true;
ftpRequest.UsePassive = true;
ftpRequest.KeepAlive = true;
ftpRequest.Method = WebRequestMethods.Ftp.DownloadFile;
ftpResponse = (FtpWebResponse)ftpRequest.GetResponse();
ftpStream = ftpResponse.GetResponseStream();
FileStream localFileStream = new FileStream(localFile, FileMode.Create);
byte[] byteBuffer = new byte[bufferSize];
int bytesRead = ftpStream.Read(byteBuffer, 0, bufferSize);
try
{
while (bytesRead > 0)
{
localFileStream.Write(byteBuffer, 0, bytesRead);
bytesRead = ftpStream.Read(byteBuffer, 0, bufferSize);
}
}
catch (Exception) { }
localFileStream.Close();
ftpStream.Close();
ftpResponse.Close();
ftpRequest = null;
}
catch (Exception) { }
return;
}
答案 7 :(得分:0)
仅供参考,Microsoft recommends not using FtpWebRequest for new development:
<块引用>我们不建议您使用 FtpWebRequest 类进行新开发。有关 FtpWebRequest 的更多信息和替代方案,请参阅 GitHub 上不应使用 WebRequest。
GitHub 链接指向 this SO page,其中包含第三方 FTP 库列表,例如 FluentFTP。