/// <summary> /// 下载文件 /// </summary> /// <param name="ftpfilepath">相对根目录的文件完整路径</param> /// <param name="filePath">本地保存文件的完整路径</param> /// <param name="fileName">文件名</param> /// <returns></returns> public bool Download(string ftpfilepath, string filePath, string fileName, out string errorinfo)////上面的代码实现了从ftp服务器下载文件的功能 { Exception ex = null; errorinfo = ""; FrmProcessBar ProcBar = new FrmProcessBar(); ProcBar.Show(); try { filePath = filePath.Replace("我的电脑\\", ""); String onlyFileName = Path.GetFileName(fileName); string newFileName = filePath; if (File.Exists(newFileName)) { errorinfo = string.Format("本地文件{0}已存在,无法下载", newFileName); return(false); } ftpfilepath = ftpfilepath.Replace("\\", "/"); string url = "ftp://" + ftpServerIP + "/" + ftpfilepath; Connect(url);//连接 reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword); reqFTP.KeepAlive = false; reqFTP.Timeout = 10000; FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse(); Stream ftpStream = response.GetResponseStream(); long cl = response.ContentLength; int bufferSize = 2048; int readCount; byte[] buffer = new byte[bufferSize]; readCount = ftpStream.Read(buffer, 0, bufferSize); FileStream outputStream = new FileStream(newFileName, FileMode.Create); long ProcBarValue = 0; ////// long all = GetFileSize(ftpfilepath, out ex); { if (null != ex) { return(false); } } ProcBar.SetFrmProcessBarMax(all / 2048); ////// while (readCount > 0) { ProcBarValue++; ProcBar.SetFrmProcessBarText("正在下载文件:" + fileName); ProcBar.SetFrmProcessBarValue(ProcBarValue); Application.DoEvents(); outputStream.Write(buffer, 0, readCount); readCount = ftpStream.Read(buffer, 0, bufferSize); } ftpStream.Close(); outputStream.Close(); response.Close(); errorinfo = "Succeed"; return(true); } catch (Exception exx) { errorinfo = string.Format("因{0},无法下载", exx.Message); return(false); } finally { ProcBar.Dispose(); ProcBar.Close(); } }
/// <summary> /// 续传文件 /// </summary> /// <param name="filename"></param> public bool Upload(string filename, long size, string path, out string errorinfo) //上面的代码实现了从ftp服务器上载文件的功能 { FrmProcessBar frmbar = new FrmProcessBar(); frmbar.Show(); path = path.Replace("\\", "/"); FileInfo fileInf = new FileInfo(filename); //string uri = "ftp://" + path + "/" + fileInf.Name; string uri = "ftp://" + ftpServerIP + "/" + path + filename; Connect(uri);//连接 // 默认为true,连接不会被关闭 // 在一个命令之后被执行 reqFTP.KeepAlive = false; reqFTP.Timeout = 10000; // 指定执行什么命令 reqFTP.Method = WebRequestMethods.Ftp.AppendFile; // 上传文件时通知服务器文件的大小 reqFTP.ContentLength = fileInf.Length; // 缓冲大小设置为kb long procMax = (fileInf.Length - size) / 2048; frmbar.SetFrmProcessBarMax(procMax); frmbar.SetFrmProcessBarText("正在续传文件:" + filename); int buffLength = 2048; byte[] buff = new byte[buffLength]; int contentLen; // 打开一个文件流(System.IO.FileStream) 去读上传的文件 FileStream fs = fileInf.OpenRead(); int value = 0; try { StreamReader dsad = new StreamReader(fs); fs.Seek(size, SeekOrigin.Begin); // 把上传的文件写入流 Stream strm = reqFTP.GetRequestStream(); // 每次读文件流的kb contentLen = fs.Read(buff, 0, buffLength); // 流内容没有结束 while (contentLen != 0) { // 把内容从file stream 写入upload stream value++; frmbar.SetFrmProcessBarValue(value); Application.DoEvents(); strm.Write(buff, 0, contentLen); contentLen = fs.Read(buff, 0, buffLength); } // 关闭两个流 strm.Close(); fs.Close(); errorinfo = "Succeed"; return(true); } catch (Exception ex) { errorinfo = string.Format("因{0},无法完成上传", ex.Message); return(false); } finally { if (null != fileInf) { fileInf = null; } frmbar.Close(); frmbar.Dispose(); } }