如何使用 FTP 在目录之间移动文件? [英] How can I use FTP to move files between directories?

查看:109
本文介绍了如何使用 FTP 在目录之间移动文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个程序需要在 FTP 服务器上将文件从一个目录移动到另一个目录.例如,文件在:

I have a program that needs to move a file from one directory to another on an FTP server. For example, the file is in:

ftp://1.1.1.1/MAIN/Dir1

我需要将文件移动到:

ftp://1.1.1.1/MAIN/Dir2

我发现了几篇推荐使用重命名命令的文章,所以我尝试了以下操作:

I found a couple of articles recommending use of the Rename command, so I tried the following:

    Uri serverFile = new Uri("ftp://1.1.1.1/MAIN/Dir1/MyFile.txt");
    FtpWebRequest reqFTP= (FtpWebRequest)FtpWebRequest.Create(serverFile);
    reqFTP.Method = WebRequestMethods.Ftp.Rename;
    reqFTP.UseBinary = true;
    reqFTP.Credentials = new NetworkCredential(ftpUser, ftpPass);
    reqFTP.RenameTo = "ftp://1.1.1.1/MAIN/Dir2/MyFile.txt";

    FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();

但这似乎不起作用 - 我收到以下错误:

But this doesn’t seem to work – I get the following error:

远程服务器返回错误:(550) 文件不可用(例如,找不到文件,无法访问).

The remote server returned an error: (550) File unavailable (e.g., file not found, no access).

起初我以为这可能与权限有关,但据我所知,我拥有整个 FTP 站点的权限(它在我的本地 PC 上,uri 解析为 localhost).

At first I thought this might relate to permissions, but as far as I can see, I have permissions to the entire FTP site (it is on my local PC and the uri is resolved to localhost).

是否应该可以像这样在目录之间移动文件,如果不能,那怎么可能?

Should it be possible to move files between directories like this, and if not, how is it possible?

解决一些已经提出的观点/建议:

To address some of the point / suggestions that have been raised:

  1. 我可以从源目录下载相同的文件,所以它肯定存在(我正在做的是先下载文件,然后将它移到其他地方).
  2. 我可以从浏览器(源目录和目标目录)访问 ftp 站点
  3. ftp 服务器在我本地机器上我自己的 IIS 实例下运行.
  4. 路径和大小写正确,没有特殊字符.

此外,我尝试将目录路径设置为:

Additionally, I have tried setting the directory path to be:

ftp://1.1.1.1/%2fMAIN/Dir1/MyFile.txt

对于源路径和目标路径 - 但这也没有区别.

Both for the source and target path - but this makes no difference either.

我发现这篇文章,似乎在说将目标指定为相对路径会有所帮助 - 似乎不可能将绝对路径指定为目标.

I found this article, which seems to say that specifying the destination as a relative path would help - it doesn't appear to be possible to specify an absolute path as the destination.

reqFTP.RenameTo = "../Dir2/MyFile.txt";

推荐答案

MSDN 似乎表明您的路径被认为是相对的,因此它尝试使用提供的凭据登录到 FTP 服务器,然后将当前目录设置为 /path 目录.如果这不是您的文件所在的目录,您将收到 550 错误.

MSDN seems to suggest that your path is considered relative, and therefore it tries to log in to the FTP server using the supplied credentials, then sets the current directory to the <UserLoginDirectory>/path directory. If this isn't the same directory where your file is, you'll get a 550 error.

这篇关于如何使用 FTP 在目录之间移动文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆