java ftp 重命名文件,当前用户目录与root不同时,无法使用ftp方法重命名文件
发布日期:2021-06-24 11:54:24 浏览次数:3 分类:技术文章

本文共 3832 字,大约阅读时间需要 12 分钟。

备注:由于垃圾邮件防范机制,我被迫将Uris的开头从ftp://替换为ftp .

我有以下问题 . 我必须使用C#ftp方法上传文件,然后重命名它 . 容易,对吗? :)

好吧,假设我的ftp主机是这样的:

ftp.contoso.com

登录后,当前目录设置为:

用户/名称

所以,我想要实现的是登录,将文件作为file.ext.tmp上传到当前目录,上传成功后,将文件重命名为file.ext

正如我猜想的那样,整个难题是为FtpWebRequest正确设置请求Uri .

MSDN声明:

URI可以是相对的或绝对的 . 如果URI的格式为“ftp://contoso.com/%2fpath”(%2f是转义'/'),那么URI是绝对的,当前目录是/ path . 但是,如果URI的格式为“ftp://contoso.com/path”,则首先.NET Framework登录到FTP服务器(使用Credentials属性设置的用户名和密码),然后是当前目录设置为UserLoginDirectory / path .

好的,所以我上传了带有以下URI的文件:

ftp.contoso.com/file.ext.tmp

好的,文件落在我想要的位置:在目录“users / name”中

现在,我想重命名该文件,因此我使用以下Uri创建Web请求:

ftp.contoso.com/file.ext.tmp

并指定重命名为参数:

file.ext

这给了我550错误:找不到文件,没有权限等 .

我在Microsoft网络监视器中跟踪了它,它给了我:

命令:RNFR,从CommandParameter重命名:/file.ext.tmp Ftp:响应端口53724,'550 File /file.ext.tmp not found'

好像是在根目录中查找文件 - 而不是在当前目录中 .

我使用Total Commander手动重命名了文件,唯一的区别是CommandParameter没有第一个斜杠:

CommandParameter:file.ext.tmp

我可以通过提供以下绝对URI来成功重命名该文件:

ftp.contoso.com/%2fusers/%2fname/file.ext.tmp

但我不喜欢这种方法,因为我必须知道当前用户目录的名称 . 它可以通过使用WebRequestMethods.Ftp.PrintWorkingDirectory来完成,但它增加了额外的复杂性(调用此方法来检索目录名,然后组合路径以形成正确的URI) .

我不明白为什么URI ftp.contoso.com/file.ext.tmp适合上传而不是重命名?我在这里错过了什么吗?

该项目设置为.NET 4.0,在Visual Studio 2010中编码 .

编辑

好的,我放置了代码片段 .

请注意,应填写ftp主机,用户名和密码 . 要使此示例正常工作 - 即产生错误 - 用户目录必须与root不同(“pwd” - 命令应返回与“/”不同的内容)

class Program

{

private const string fileName = "test.ext";

private const string tempFileName = fileName + ".tmp";

private const string ftpHost = "127.0.0.1";

private const string ftpUserName = "anonymous";

private const string ftpPassword = "";

private const int bufferSize = 524288;

static void Main(string[] args)

{

try

{

string path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), fileName);

if (!File.Exists(path))

File.WriteAllText(path, "FTP RENAME SAMPLE");

string requestUri = "ftp://" + ftpHost + "/" + tempFileName;

//upload

FtpWebRequest uploadRequest = (FtpWebRequest)WebRequest.Create(requestUri);

uploadRequest.UseBinary = true;

uploadRequest.UsePassive = true;

uploadRequest.Credentials = new NetworkCredential(ftpUserName, ftpPassword);

uploadRequest.KeepAlive = true;

uploadRequest.Method = WebRequestMethods.Ftp.UploadFile;

Stream requestStream = null;

FileStream localFileStream = null;

localFileStream = File.OpenRead(path);

requestStream = uploadRequest.GetRequestStream();

byte[] buffer = new byte[bufferSize];

int readCount = localFileStream.Read(buffer, 0, bufferSize);

long bytesSentCounter = 0;

while (readCount > 0)

{

requestStream.Write(buffer, 0, readCount);

bytesSentCounter += readCount;

readCount = localFileStream.Read(buffer, 0, bufferSize);

System.Threading.Thread.Sleep(100);

}

localFileStream.Close();

requestStream.Close();

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

FtpStatusCode code = response.StatusCode;

string description = response.StatusDescription;

response.Close();

if (code == FtpStatusCode.ClosingData)

Console.WriteLine("File uploaded successfully");

//rename

FtpWebRequest renameRequest = (FtpWebRequest)WebRequest.Create(requestUri);

renameRequest.UseBinary = true;

renameRequest.UsePassive = true;

renameRequest.Credentials = new NetworkCredential(ftpUserName, ftpPassword);

renameRequest.KeepAlive = true;

renameRequest.Method = WebRequestMethods.Ftp.Rename;

renameRequest.RenameTo = fileName;

try

{

FtpWebResponse renameResponse = (FtpWebResponse)renameRequest.GetResponse();

Console.WriteLine("Rename OK, status code: {0}, rename status description: {1}", response.StatusCode, response.StatusDescription);

renameResponse.Close();

}

catch (WebException ex)

{

Console.WriteLine("Rename failed, status code: {0}, rename status description: {1}", ((FtpWebResponse)ex.Response).StatusCode,

((FtpWebResponse)ex.Response).StatusDescription);

}

}

catch (Exception ex)

{

Console.WriteLine(ex.ToString());

}

finally

{

Console.ReadKey();

}

}

}

转载地址:https://blog.csdn.net/weixin_32601635/article/details/115989311 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:用php写一个时钟效果,用HTML5的canvas实现一个炫酷时钟效果
下一篇:matlab工程热力学大程序,西安交通大学工程期末编程大作业(完整版

发表评论

最新留言

初次前来,多多关照!
[***.217.46.12]2024年04月06日 22时25分47秒

关于作者

    喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!

推荐文章