c# ftp upload to Linux

本文关键字:Linux to upload ftp | 更新日期: 2023-09-27 18:13:34

我试图检查FTP服务器上是否存在目录。在你说"使用ListDirectory"或"使用PrintWorkingDirectory"之前,它们并不总是有效;例如,我测试了ftp://webserver/Logs是否存在,两者都告诉我它存在,而实际上它不存在。所以我选择了上传一个文件到这个目录,如果成功,那么这个目录就存在。

问题是,下面的方法不适用于GoDaddy基于CentOS的服务器运行vsFTPd 2.0.7.2。

所以我用Wireshark监控流量,用Filezilla看看它在做什么,我的应用程序不能使它工作。唯一的区别是Filezilla正在改变工作目录,当我试图上传文件时,在它之前有一个路径。

我有一种感觉,这与它上传到服务器的路径和Linux的解释有关,因为它的名字可能有点有趣…有什么想法欢迎提出来吗?

<

应用程序代码/strong>

private bool DirectoryExists(string d)
{
    bool exists = true;
    try
    {
        string file = "directoryexists.test";
        string path = url + homepath + d + "/" + file;
        //Try to save to the directory
        req = (FtpWebRequest)WebRequest.Create(path);
        req.ConnectionGroupName = "conngroup1";
        req.Method = WebRequestMethods.Ftp.UploadFile;
        if (nc != null) req.Credentials = nc;
        if (cbSSL.Checked) req.EnableSsl = true;
        req.Timeout = 10000;
        byte[] fileContents = System.Text.Encoding.Unicode.GetBytes("SAFE TO DELETE");
        req.ContentLength = fileContents.Length;
        Stream s = req.GetRequestStream();
        s.Write(fileContents, 0, fileContents.Length);
        s.Close();
        //Delete file if successful
        req = (FtpWebRequest)WebRequest.Create(path);
        req.ConnectionGroupName = "conngroup1";
        req.Method = WebRequestMethods.Ftp.DeleteFile;
        if (nc != null) req.Credentials = nc;
        if (cbSSL.Checked) req.EnableSsl = true;
        req.Timeout = 10000;
        res = (FtpWebResponse)req.GetResponse();
        res.Close();
    }
    catch (WebException ex)
    {
        exists = false;
    }
    return exists;
}

Filezilla log through Wireshark

Response: 230 Login successful.
Request: CWD /Home/test1
Response: 250 Directory successfully changed.
Request: TYPE I
Response: 200 Switching to Binary mode.
Request: PASV
Response: 227 Entering Passive Mode (216,69,186,142,71,209)
Request: LIST
Response: 150 Here comes the directory listing.
FTP Data: 78 bytes
Response: 226 Directory send OK.
Request: PASV
Response: 227 Entering Passive Mode (216,69,186,142,177,1)
Request: STOR directoryexists.txt
Response: 150 Ok to send data.
Response: 226 File receive OK.

App log through Wireshark

Response: 230 Login successful.
Request: OPTS utf8 on
Response: 501 Option not understood.
Request: PWD
Response: 257 "/Home/"
Request: PWD
Response: 257 "/Home/"
Request: TYPE I
Response: 200 Switching to Binary mode.
Request: PASV
Response: 227 Entering Passive Mode (216,69,186,142,217,87)
Request: STOR test1/directoryexists.txt
Response: 553 Could not create file.

如果文件夹不存在,则创建文件夹。

Response: 230 Login successful.
Request: PWD
Response: 257 "/Home/"
Request: PWD
Response: 257 "/Home/"
Request: TYPE I
Response: 200 Switching to Binary mode.
Request: PASV
Response: 227 Entering Passive Mode (216,69,186,142,220,60)
Request: STOR Logs/directoryexists.txt
Response: 553 Could not create file.
Request: PWD
Response: 257 "/Home/"
Request: MKD Logs
Response: 257 Create folder operation successful.
Request: TYPE I
Response: 200 Switching to Binary mode.
Request: PASV
Response: 227 Entering Passive Mode (216,69,186,142,255,245)
Request: STOR Logs/LogFiles/directoryexists.txt
Response: 553 Could not create file.
Request: PWD
Response: 257 "/Home/"
Request: MKD Logs/LogFiles
Response: 257 Create folder operation successful.

c# ftp upload to Linux

Linux又来了…

解决方案是在路径名中设置一个双斜杠,这样当涉及到STOR时,它就有一个前导斜杠…像这样:

string url = "ftp://website/";
string homepath = "/Home/";
string d = "test1";
string file = "directoryexists.test";
string path = url + homepath + d + "/" + file;

所以完整的路径看起来像ftp://website//Home/test1/directoryexists.test

req = (FtpWebRequest)WebRequest.Create("ftp://website//Home/test1/directoryexists.test"); 

这样,存储命令看起来就像

STOR /Home/test1/directoryexists.test

您可以从StatusDescription

获取主路径
req = (FtpWebRequest)WebRequest.Create(url);
req.Method = WebRequestMethods.Ftp.PrintWorkingDirectory;
if (nc != null) req.Credentials = nc;
if (cbSSL.Checked) req.EnableSsl = true;
req.Timeout = 10000;
res = (FtpWebResponse)req.GetResponse();
System.Text.RegularExpressions.Regex regexp = new System.Text.RegularExpressions.Regex("''s'"([^'"]*)'"''s");
homepath = regexp.Match(res.StatusDescription).Groups[1].Value;
res.Close();