【问题标题】:Error while Uploading file to FTP server C#将文件上传到 FTP 服务器 C# 时出错
【发布时间】:2013-11-22 08:29:12
【问题描述】:

我正在尝试将 .txt 文件上传到 ftp 服务器。但我有一些问题。我不断收到此错误:远程服务器返回错误:(553) 文件名不允许。

这总是发生在这一行:

Stream writer = ftpReq.GetRequestStream();

我无法弄清楚问题是什么。我试过用 filezilla 上传文件,效果很好。我还尝试从服务器复制文件目标 url 并将其硬编码到我的应用程序中,但我仍然遇到相同的错误。有没有其他方法可以将文件上传到 ftp 服务器??

这是我的代码:

string localFilePath = @"\\fileprint\data\Groups\Operation\fileExports\folder\";
string archiveFilePath = @"\\fileprint\data\Groups\Operation\fileExports\folder\Archive\";
string logFilePath = @"C:\Users\lmy\Desktop\Logs";
string ftpServer = "ftp://my.server.no:21/home/pll332/tmp/";
private string logFileName = "" + DateTime.Now.Year.ToString() + "-" + DateTime.Now.Month.ToString() + "-" + DateTime.Now.Day.ToString();
string userName = "pll332";
string password = "password";

public Controller()
{
}

public void UploadFile()
{
    try
    {
        string[] files = Directory.GetFiles(localFilePath);
        foreach (string file in files)
        {
            string fileName = Path.GetFileName(file);
            FtpWebRequest ftpReq = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpServer + fileName));
            ftpReq.Method = WebRequestMethods.Ftp.UploadFile;
            ftpReq.UsePassive = true;
            ftpReq.UseBinary = true;
            ftpReq.KeepAlive = true;
            ftpReq.Credentials = new NetworkCredential(userName.Normalize(), password.Normalize());

            FileInfo fileInfo = new FileInfo(localFilePath + @"\" + fileName);
            FileStream fileStream = fileInfo.OpenRead();

            byte[] fileContent = new byte[fileInfo.Length];
            fileStream.Read(fileContent, 0, Convert.ToInt32(fileInfo.Length));

            Stream writer = ftpReq.GetRequestStream();
            writer.Write(fileContent, 0, fileContent.Length);
            fileStream.Close();
            writer.Close();
            FtpWebResponse response = (FtpWebResponse)ftpReq.GetResponse();

            AppendLogFile(response, "Uploaded Files: ", fileName);
            MoveToArchive(file, archiveFilePath + fileName);
        }

    }
    catch (Exception exception)
    {
        Console.WriteLine(exception.Message);
    }
}

希望你们能帮助我。谢谢!

编辑: 我试过这个:

            string fileName = Path.GetFileName(file);
            UriBuilder uri = new UriBuilder();
            uri.Scheme = "ftp";
            uri.Host = "my.server.no";
            uri.Port = 21;
            uri.Path = "/home/username/tmp/";
            uri.UserName = "username";
            uri.Password = "password";
            FtpWebRequest ftpReq = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri.ToString() + fileName));
            ftpReq.Method = WebRequestMethods.Ftp.UploadFile;
            ftpReq.UsePassive = true;
            ftpReq.UseBinary = true;
            ftpReq.KeepAlive = true;
            ftpReq.Credentials = new NetworkCredential(userName.Normalize(), password.Normalize());

这给了我同样的错误......

【问题讨论】:

  • 您可以尝试使用第三方组件,例如enterprisedt.com/products/edtftpnet
  • 你尝试了什么?当您收到错误消息时,ftpServer + fileName 的实际值是多少?
  • ftp://my.server.no:21/home/username/tmp/01002589006741.TXT 这是 ftpserver+filename 的值。使用 UriBuilder 后,它看起来像这样。 ftp://username:password@my.server.no:21/home/username/tmp/01002589006741.TXT Stille 得到同样的错误..

标签: c# .net ftp stream ftpwebrequest


【解决方案1】:

我终于找到了错误的原因。我试图上传到的服务器似乎是一个 linux 服务器。我在论坛中找到了有关如何解决此问题的说明。这是在论坛上写的:

这可能对 Linux FTP 服务器有所帮助。

因此,Linux FTP 服务器与 IIS 不同,没有通用的 FTP 根目录。相反,当您使用某个用户的凭据登录 FTP 服务器时,会使用该用户的根目录。因此 FTP 目录层次结构从 /root/ 开始,用于 root 用户,从 /home/username 开始用于其他用户。 因此,如果您需要查询的文件不是相对于用户帐户主目录,而是相对于文件系统根目录,请在服务器名称后添加一个额外的/。生成的 URL 如下所示:

ftp://servername.net//var/lalala

所以当我从以下位置更改连接 Uri 时:

ftp://username:password@my.server.no:21/home/username/tmp/

到:

ftp://username:password@my.server.no:21//home/username/tmp/

(请注意,我在 /home/username/tmp/ 之前添加了一个额外的 /

然后我工作了!!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-08-08
    • 2017-02-27
    • 1970-01-01
    • 1970-01-01
    • 2011-02-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多