用ftpwebrequest的方式写了一个从unxi上ftp下载文件的方法代码如下。
用root用户可以下载到文件,但是改用其他用户会在代码
FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
这行抛出WebException的错误。错误信息是550。 单独从cmd连接ftp用上面用户(非root用户)get文件是可以的get到的。(说明该用户有ftp权限) 这是什么原因呢??????? FtpWebRequest reqFTP;
try
{
FileStream outputStream = new FileStream(filePath + "\\" + fileName, FileMode.Create);
reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + ftpServerIP + "/" + fileName));
reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;
reqFTP.UseBinary = true;
reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
Stream ftpStream = response.GetResponseStream();
long cl = response.ContentLength;
FileStream outputStream = File.Create(currentPath);
int bufferSize = 2048;
int readCount;
byte[] buffer = new byte[bufferSize];
readCount = ftpStream.Read(buffer, 0, bufferSize);
while (readCount > 0)
{
outputStream.Write(buffer, 0, readCount);
readCount = ftpStream.Read(buffer, 0, bufferSize);
}
ftpStream.Close();
outputStream.Close();
response.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
用root用户可以下载到文件,但是改用其他用户会在代码
FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
这行抛出WebException的错误。错误信息是550。 单独从cmd连接ftp用上面用户(非root用户)get文件是可以的get到的。(说明该用户有ftp权限) 这是什么原因呢??????? FtpWebRequest reqFTP;
try
{
FileStream outputStream = new FileStream(filePath + "\\" + fileName, FileMode.Create);
reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + ftpServerIP + "/" + fileName));
reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;
reqFTP.UseBinary = true;
reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
Stream ftpStream = response.GetResponseStream();
long cl = response.ContentLength;
FileStream outputStream = File.Create(currentPath);
int bufferSize = 2048;
int readCount;
byte[] buffer = new byte[bufferSize];
readCount = ftpStream.Read(buffer, 0, bufferSize);
while (readCount > 0)
{
outputStream.Write(buffer, 0, readCount);
readCount = ftpStream.Read(buffer, 0, bufferSize);
}
ftpStream.Close();
outputStream.Close();
response.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
连接后先切换到跟目录,在去指定目录取得就行了,这个是够就是/指定目录
例如,我用ftpuser登录会进入/home/ftpuser/目录下,而不会想root用户直接登录到根目录,
这个问题同样困扰我,还想找资料中
如5楼所说的要是用ftpuser登录会进入/home/ftpuser/目录下,而不会想root用户直接登录到根目录
所以用其他用户前需要先跳到根目录下。
具体方法是用转义字符/%2E%2E这个相当于/..
ftpuser用户的目录有两层则需要跳出两次。在uri的路径前加两个/%2E%2E如 ftp://ip地址/%2E%2E/%2E%2E要下载的文件地址。现在的问题就是怎么知道该用户的默认目录有几层了。
WebQuestMethods.Ftp.PrintWorkingDirectory这个方法就有用了哦!
http://blogs.msdn.com/mariya/archive/2006/03/06/544523.aspx