C#下载FTP文件时,文件名如何更改

Rabbitmo 2013-03-01 09:43:23
现有如下问题
FTP文件 :02/28/2013 10:57上午 10 新建 文本文档.txt
链接:ftp://****.net/%D0%C2%BD%A8%20%CE%C4%B1%BE%CE%C4%B5%B5.txt
当我下载文件时,文件名是"%D0%C2%BD%A8%20%CE%C4%B1%BE%CE%C4%B5%B5.txt"
文件名默认就是连接的文件名,既不方便管理也不直观;
现在我想要把特殊字符换成中文
请教各位有什么方法
代码如下:

string URL = ftpServerIP;
string newURL = string.Empty;
//if (URL.Contains(@"\"))
//{
// newURL= URL.Replace('\'', '/');
//}
int n = URL.LastIndexOf('/');
string URLAddress = URL.Substring(0, n);
string file = URL.Substring(n + 1, URL.Length - n - 1);
string Dir = filePath;
string Path = Dir + '\\' + file;

FileStream outputStream = new FileStream(Path, FileMode.Create);
string[] arrfn = file.Split(new char[] { '.' }, StringSplitOptions.None);
//Encoding ec = Encoding.Default;

string fn = System.Web.HttpUtility.UrlDecode(arrfn[0], System.Text.Encoding.GetEncoding("GB2312"));//这里要转码,否则提示FTP无效

reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(URLAddress + "/" + fn + "." + arrfn[1]));//文件名转码后重新拼接链接。否则提示FTP无效。
reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;

reqFTP.UseBinary = true;
reqFTP.KeepAlive = false;
reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);

FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
StreamReader sr = new StreamReader(response.GetResponseStream());
Stream ftpStream = response.GetResponseStream();
long cl = response.ContentLength;
int bufferSize = 2048;
int readCount;
byte[] buffer = new byte[bufferSize];
readCount = ftpStream.Read(buffer, 0, bufferSize);
//string ff = sr.ReadLine();
while (readCount > 0)
{
outputStream.Write(buffer, 0, readCount);
readCount = ftpStream.Read(buffer, 0, bufferSize);
}
reqFTP.Method = WebRequestMethods.Ftp.Rename;
reqFTP.RenameTo = Dir + fn + "." + arrfn[1];
ftpStream.Close();
outputStream.Close();
response.Close();
return 0;
...全文
274 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
Rabbitmo 2013-03-01
  • 打赏
  • 举报
回复
quote]gxingmin 回复于: 2013-03-01 09:48:07 string Path = Dir + '\\' + file; 这里的file变量,你想改啥就改啥 [/quote] 多谢指教。 昨天搞了一天,你给我一句代码就解决了。 看来真的不能自己闷头苦恼地写代码。
EnForGrass 2013-03-01
  • 打赏
  • 举报
回复
你一看这方法,也知道路径该怎么设置,即使你不知道,调试一下,也可了解设置路径部分代码 修改成你要指定的路径
Rabbitmo 2013-03-01
  • 打赏
  • 举报
回复
引用 楼主 Rabbitmo 的回复:
现有如下问题 FTP文件 :02/28/2013 10:57上午 10 新建 文本文档.txt 链接:ftp://****.net/%D0%C2%BD%A8%20%CE%C4%B1%BE%CE%C4%B5%B5.txt 当我下载文件时,文件名是"%D0%C2%BD%A8%20%CE%C4%B1%BE%CE%C4%B5%B5.txt" 文件名默认就是连接的文件名,既不……
谢谢,先试试
gxingmin 2013-03-01
  • 打赏
  • 举报
回复
string Path = Dir + '\\' + file; 这里的file变量,你想改啥就改啥

110,534

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧