gpt4 book ai didi

c# ftp 上传到 Linux

转载 作者:行者123 更新时间:2023-11-30 16:27:22 24 4
gpt4 key购买 nike

我正在尝试检查 FTP 服务器上是否存在目录。在您说“使用 ListDirectory”或“使用 PrintWorkingDirectory”之前,它们并不总是有效;例如,我测试了是否 ftp://webserver/Logs存在,并且都告诉我它确实存在,但实际上并不存在。所以我沿着将文件上传到目录的路线走下去,如果成功,那么目录就存在。

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

因此,我使用 Wireshark 监控流量并使用 Filezilla 查看它在做什么,而我的应用程序无法使其正常工作。唯一的区别是 Filezilla 正在更改工作目录,因为我正在尝试使用它之前的路径上传文件。

我觉得它与上传到服务器的路径和 Linux 的解释有关,因为它的名称可能有点有趣...:-D 热烈欢迎任何想法?

应用代码

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;
}

通过 Wireshark 的 Filezilla 日志

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.

通过 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.

最佳答案

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 命令看起来像

STOR /Home/test1/directoryexists.test

您可以从 StatusDescription 中获取 Home 路径

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();

关于c# ftp 上传到 Linux,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7987829/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com