FTP无法连接,无法下载整个文件夹 [英] FTP not connect and cannot download entire folder

查看:81
本文介绍了FTP无法连接,无法下载整个文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

错误消息是:此FTP命令的请求URL无效
错误消息是:远程服务器记录了错误(550)文件不可用(例如,文件未找到,无法访问).

这里的代码:

使用系统;
使用System.Collections.Generic;
使用System.ComponentModel;
使用System.Data;
使用System.Drawing;
使用System.Linq;
使用System.Text;
使用System.Windows.Forms;
使用System.Net;
使用System.IO;

命名空间WindowsFormsApplication1
{
公共局部类Form1:Form
{

公用字符串ftpServerIP;
公用字符串ftpUserID;
公用字符串ftpPassword;
公共字符串remoteDir;
公共字符串localDestnDir;

公共Form1()
{
InitializeComponent();
}

private void button1_Click(对象发送者,EventArgs e)
{

ftpServerIP ="xxxx";
ftpUserID ="xxxxxx";
ftpPassword ="xxx";
remoteDir ="Test1";
localDestnDir ="C:\\";


字符串[]文件= GetFileList();
foreach(文件中的字符串文件)
{
下载(文件);
}
}

公共字符串[] GetFileList()
{
字符串[] downloadFiles;
StringBuilder结果=新的StringBuilder();
Web响应响应= null;
StreamReader reader = null;
试试
{
FtpWebRequest reqFTP;
reqFTP =(FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + ftpServerIP +"/"));
reqFTP.UseBinary = true;
reqFTP.Credentials =新的NetworkCredential(ftpUserID,ftpPassword);
reqFTP.Method = WebRequestMethods.Ftp.ListDirectory;
reqFTP.Proxy = null;
reqFTP.KeepAlive = false;
reqFTP.UsePassive = false;
响应= reqFTP.GetResponse();
reader = new StreamReader(response.GetResponseStream());
字符串行= reader.ReadLine();
而(行!= null)
{
result.Append(line);
result.Append("\ n");
行= reader.ReadLine();
}
//删除结尾的"\ n"
result.Remove(result.ToString().LastIndexOf(''\ n''),1);
返回result.ToString().Split(''\ n'');
}
catch(ex ex例外)
{
如果(读者!= null)
{
reader.Close();
}
如果(响应!=空)
{
response.Close();
}
downloadFiles = null;
返回downloadFiles;
}
}

私人void下载(字符串文件)
{
试试
{
字符串uri ="ftp://" + ftpServerIP +"/" + remoteDir +"/" +文件;
Uri serverUri =新的Uri(uri);
如果(serverUri.Scheme!= Uri.UriSchemeFtp)
{
返回;
}
FtpWebRequest reqFTP;
reqFTP =(FtpWebRequest)FtpWebRequest.Create(新的Uri("ftp://" + ftpServerIP +"/" + remoteDir +"/" +文件));
reqFTP.Credentials =新的NetworkCredential(ftpUserID,ftpPassword);
reqFTP.KeepAlive = false;
reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;
reqFTP.UseBinary = true;
reqFTP.Proxy = null;
reqFTP.UsePassive = false;
FtpWebResponse响应=(FtpWebResponse)reqFTP.GetResponse();
流responseStream = response.GetResponseStream();
FileStream writeStream =新的FileStream(localDestnDir +"/" +文件,FileMode.Create);
int长度= 2048;
Byte []缓冲区=新的Byte [Length];
int bytesRead = responseStream.Read(buffer,0,Length);
while(bytesRead> 0)
{
writeStream.Write(buffer,0,bytesRead);
bytesRead = responseStream.Read(buffer,0,Length);
}
writeStream.Close();
response.Close();
}
捕获(WebException wEx)
{
MessageBox.Show(wEx.Message,下载错误");
}
catch(ex ex例外)
{
MessageBox.Show(例如消息,下载错误");
}
}







}
}

Error msg is:The request URL is invalid this FTP command
Error msg is:the remote server returen an error(550)file unavailable(e.g,file not found,no access).

here that code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.IO;

namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{

public string ftpServerIP;
public string ftpUserID;
public string ftpPassword;
public string remoteDir;
public string localDestnDir;

public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{

ftpServerIP = "xxxx";
ftpUserID = "xxxxxx";
ftpPassword = "xxx";
remoteDir = "Test1";
localDestnDir="C:\\";


string[] files = GetFileList();
foreach (string file in files)
{
Download(file);
}
}

public string[] GetFileList()
{
string[] downloadFiles;
StringBuilder result = new StringBuilder();
WebResponse response = null;
StreamReader reader = null;
try
{
FtpWebRequest reqFTP;
reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + ftpServerIP + "/"));
reqFTP.UseBinary = true;
reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
reqFTP.Method = WebRequestMethods.Ftp.ListDirectory;
reqFTP.Proxy = null;
reqFTP.KeepAlive = false;
reqFTP.UsePassive = false;
response = reqFTP.GetResponse();
reader = new StreamReader(response.GetResponseStream());
string line = reader.ReadLine();
while (line != null)
{
result.Append(line);
result.Append("\n");
line = reader.ReadLine();
}
// to remove the trailing ''\n''
result.Remove(result.ToString().LastIndexOf(''\n''), 1);
return result.ToString().Split(''\n'');
}
catch (Exception ex)
{
if (reader != null)
{
reader.Close();
}
if (response != null)
{
response.Close();
}
downloadFiles = null;
return downloadFiles;
}
}

private void Download(string file)
{
try
{
string uri = "ftp://" + ftpServerIP + "/" + remoteDir + "/" + file;
Uri serverUri = new Uri(uri);
if (serverUri.Scheme != Uri.UriSchemeFtp)
{
return;
}
FtpWebRequest reqFTP;
reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + ftpServerIP + "/" + remoteDir + "/" + file));
reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
reqFTP.KeepAlive = false;
reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;
reqFTP.UseBinary = true;
reqFTP.Proxy = null;
reqFTP.UsePassive = false;
FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
Stream responseStream = response.GetResponseStream();
FileStream writeStream = new FileStream(localDestnDir + "/" + file, FileMode.Create);
int Length = 2048;
Byte[] buffer = new Byte[Length];
int bytesRead = responseStream.Read(buffer, 0, Length);
while (bytesRead > 0)
{
writeStream.Write(buffer, 0, bytesRead);
bytesRead = responseStream.Read(buffer, 0, Length);
}
writeStream.Close();
response.Close();
}
catch (WebException wEx)
{
MessageBox.Show(wEx.Message, "Download Error");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Download Error");
}
}







}
}

推荐答案

这是一大堆代码,您如何编辑帖子并指出您遇到问题的代码呢?如果无法连接,则显然无法下载任何内容.


This is a huge blob of code, how about you edit your post and point out hte code that you have problems with ? If you can''t connect, you plainly can''t download anything.


写道:​​

result.Append(line);
result.Append("\ n");

result.Append(line);
result.Append("\n");



这不会做您希望的. result.AppendLine,将.



This will not do what you hope. result.AppendLine, will.


这篇关于FTP无法连接,无法下载整个文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆