将文件上传到FTP服务器C#时出错

时间:2013-11-22 08:29:13

标签: c# .net ftp stream ftpwebrequest

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

这总是发生在这一行:

Stream writer = ftpReq.GetRequestStream();

我无法弄清问题是什么。我尝试使用filezilla上传文件,并且工作正常。我也试图从服务器复制文件目标网址并将其编码到我的应用程序中,但我仍然得到相同的错误。还有其他方法可以将文件上传到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());

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

1 个答案:

答案 0 :(得分:2)

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

这可能有助于Linux FTP服务器。

因此,与IIS不同的Linux FTP服务器没有通用的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 / 之前添加了额外的 /

然后我工作了!!