如果您只有绝对路径呢?
好的,我遇到了这篇文章,因为我也遇到了相同的错误。答案似乎是使用相对路径,但这不是解决我的问题的好方法,因为我得到的文件夹路径是绝对路径字符串。
我随意想出的解决方案有效,但非常丑陋。我将把这个变成社群维基答案,如果有更好的解决方案,请随时编辑它。
自从我学会这个以后,我有两种解决方案。
从移动到路径中获取绝对路径,并将其转换为相对URL。
public static string GetRelativePath(string ftpBasePath, string ftpToPath)
{
if (!ftpBasePath.StartsWith("/"))
{
throw new Exception("基本路径不是绝对路径");
}
else
{
ftpBasePath = ftpBasePath.Substring(1);
}
if (ftpBasePath.EndsWith("/"))
{
ftpBasePath = ftpBasePath.Substring(0, ftpBasePath.Length - 1);
}
if (!ftpToPath.StartsWith("/"))
{
throw new Exception("目标路径不是绝对路径");
}
else
{
ftpToPath = ftpToPath.Substring(1);
}
if (ftpToPath.EndsWith("/"))
{
ftpToPath = ftpToPath.Substring(0, ftpToPath.Length - 1);
}
string[] arrBasePath = ftpBasePath.Split("/".ToCharArray());
string[] arrToPath = ftpToPath.Split("/".ToCharArray());
int basePathCount = arrBasePath.Count();
int levelChanged = basePathCount;
for (int iIndex = 0; iIndex < basePathCount; iIndex++)
{
if (arrToPath.Count() > iIndex)
{
if (arrBasePath[iIndex] != arrToPath[iIndex])
{
levelChanged = iIndex;
break;
}
}
}
int HowManyBack = basePathCount - levelChanged;
StringBuilder sb = new StringBuilder();
for (int i = 0; i < HowManyBack; i++)
{
sb.Append("../");
}
for (int i = levelChanged; i < arrToPath.Count(); i++)
{
sb.Append(arrToPath[i]);
sb.Append("/");
}
return sb.ToString();
}
public static string MoveFile(string ftpuri, string username, string password, string ftpfrompath, string ftptopath, string filename)
{
string retval = string.Empty;
FtpWebRequest ftp = (FtpWebRequest)FtpWebRequest.Create(ftpuri + ftpfrompath + filename);
ftp.Method = WebRequestMethods.Ftp.Rename;
ftp.Credentials = new NetworkCredential(username, password);
ftp.UsePassive = true;
ftp.RenameTo = GetRelativePath(ftpfrompath, ftptopath) + filename;
Stream requestStream = ftp.GetRequestStream();
FtpWebResponse ftpresponse = (FtpWebResponse)ftp.GetResponse();
Stream responseStream = ftpresponse.GetResponseStream();
StreamReader reader = new StreamReader(responseStream);
return reader.ReadToEnd();
}
或者...
从“ftp from”路径下载文件,上传到“ftp to”路径并从“ftp from”路径中删除。
public static string SendFile(string ftpuri, string username, string password, string ftppath, string filename, byte[] datatosend)
{
if (ftppath.Substring(ftppath.Length - 1) != "/")
{
ftppath += "/";
}
FtpWebRequest ftp = (FtpWebRequest)FtpWebRequest.Create(ftpuri + ftppath + filename);
ftp.Method = WebRequestMethods.Ftp.UploadFile;
ftp.Credentials = new NetworkCredential(username, password);
ftp.UsePassive = true;
ftp.ContentLength = datatosend.Length;
Stream requestStream = ftp.GetRequestStream();
requestStream.Write(datatosend, 0, datatosend.Length);
requestStream.Close();
FtpWebResponse ftpresponse = (FtpWebResponse)ftp.GetResponse();
return ftpresponse.StatusDescription;
}
public static string DeleteFile(string ftpuri, string username, string password, string ftppath, string filename)
{
FtpWebRequest ftp = (FtpWebRequest)FtpWebRequest.Create(ftpuri + ftppath + filename);
ftp.Method = WebRequestMethods.Ftp.DeleteFile;
ftp.Credentials = new NetworkCredential(username, password);
ftp.UsePassive = true;
FtpWebResponse response = (FtpWebResponse)ftp.GetResponse();
Stream responseStream = response.GetResponseStream();
StreamReader reader = new StreamReader(responseStream);
return reader.ReadToEnd();
}
public static string MoveFile(string ftpuri, string username, string password, string ftpfrompath, string ftptopath, string filename)
{
string retval = string.Empty;
byte[] filecontents = GetFile(ftpuri, username, password, ftpfrompath, filename);
retval += SendFile(ftpuri, username, password, ftptopath, filename, filecontents);
retval += DeleteFile(ftpuri, username, password, ftpfrompath, filename);
return retval;
}