110,534
社区成员
发帖
与我相关
我的任务
分享
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;