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

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

问题描述

我有一个需要从一个目录移动文件到另一个FTP服务器上的程序。例如,该文件是在

  ftp://1.1.1.1/MAIN/Dir1
 

和我需要将文件移动到:

  ftp://1.1.1.1/MAIN/Dir2
 

我发现一对夫妇的文章推荐使用重命名命令,所以我尝试了以下内容:

 乌里serverFile =新的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 =新的NetworkCredential(名为ftpuser,ftpPass);
    reqFTP.RenameTo =ftp://1.1.1.1/MAIN/Dir2/MyFile.txt;

    FtpWebResponse响应=(FtpWebResponse)reqFTP.GetResponse();
 

不过,这似乎并没有工作 - 我得到以下错误:

  

远程服务器返回错误:(550)文件不可用(例如,未找到文件,没有访问权限)

起初我以为这可能涉及到的权限,但据我所看到的,我有权对整个FTP站点(这是我的本地PC上的URI被解析为本地主机)。

它应该是可以这样的目录间移动文件,如果没有,怎么可能?

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

  1. 我可以从源目录中相同的文件,所以它肯定存在(我在做什么,首先下载该文件,然后移动它别的地方)。
  2. 我可以从浏览器访问FTP站点(源和目标目录)
  3. 在FTP服务器在我的本地机器上运行在我自己的IIS实例。
  4. 的路径和情况下是正确的,也没有特殊的字符。

此外,我已经尝试设置的目录路径是:

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

这两个源和目标路径 - 但是这都没有区别要么

我发现这个的文章,似乎是说,指定目标为相对路径将有助于 - 它不会出现有可能指定绝对路径作为目标

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

解决方案

<一个href="http://msdn.microsoft.com/en-us/library/system.net.ftpwebrequest.aspx#Y2296">MSDN似乎表明,你的路径被认为是相对的,因此,它会尝试使用提供的凭据登录到FTP服务器,然后将当前目录切换到&LT; UserLoginDirectory&GT; /路径目录。如果这不是在您的文件是相同的目录,你会得到一个550错误。

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

and I need to move the file to:

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:

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

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. I can download the same file from the source directory, so it definitely exists (what I'm doing is downloading the file first, and then moving it somewhere else).
  2. I can access the ftp site from a browser (both the source and target directory)
  3. The ftp server is running under my own IIS instance on my local machine.
  4. The path and case are correct and there are no special characters.

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 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天全站免登陆