public FileUpLoadHelpDemo(string FTPServiceIP, string FTPUserName, string FTPUserPwd) { FTPInfo = new FTPSeviceInfo(); FTPInfo.FTPServiceIP = FTPServiceIP; FTPInfo.FTPUserName = FTPUserName; FTPInfo.FTPUserPwd = FTPUserPwd; }
public FileUpLoadHelpDemo(string FTPServiceIP) { FTPInfo = new FTPSeviceInfo(); FTPInfo.FTPServiceIP = FTPServiceIP; FTPInfo.FTPUserName = string.Empty; FTPInfo.FTPUserPwd = string.Empty; }
/// <summary> /// FTP服务器下载文件,指定本地路径和本地文件名 /// </summary> /// <param name="remoteFileName">远程文件名</param> /// <param name="localFileName">保存本地的文件名(包含路径)</param> /// <param name="Info">服务器信息</param> /// <param name="ifCredential">是否启用身份验证(false:表示允许用户匿名下载)</param> /// <param name="brokenOpen">是否支持断点下载</param> /// <param name="updateProgress">>报告进度的处理(第一个参数:总大小,第二个参数:当前进度)</param> /// <returns></returns> public static bool FtpDownload(string remoteFileName, string localFileName, FTPSeviceInfo Info, bool ifCredential, bool brokenOpen, Action <int, int> updateProgress = null) { if (brokenOpen) { try { long size = 0; if (File.Exists(localFileName)) { using (FileStream outputStream = new FileStream(localFileName, FileMode.Open)) { size = outputStream.Length; } } return(FtpBrokenDownload(remoteFileName, localFileName, Info, ifCredential, size, updateProgress)); } catch { throw; } } else { return(FtpDownload(remoteFileName, localFileName, ifCredential, Info, updateProgress)); } }
/// <summary> /// 从FTP服务器下载文件,指定本地路径和本地文件名 /// </summary> /// <param name="remoteFileName">远程文件名</param> /// <param name="localFileName">保存本地的文件名(包含路径)</param> /// <param name="FtpUserName">用户名</param> /// <param name="FtpUserPwd">密码</param> /// <returns></returns> public static bool FtpDownload(string remoteFileName, string localFileName, string FtpUserName, string FtpUserPwd) { FTPSeviceInfo Info = new FTPSeviceInfo { FTPUserName = FtpUserName, FTPUserPwd = FtpUserPwd }; return(FtpDownload(remoteFileName, localFileName, Info)); }
/// <summary> ///在ftp服务器上创建文件目录 /// </summary> /// <param name="uri">文件目录</param> /// <param name="Info">服务器信息</param> /// <returns></returns> public static bool MakeDir(string uri, FTPSeviceInfo Info) { string url = string.Empty; var uriStr = "ftp://" + Info.FTPServiceIP + "/"; uriStr += uri; bool b = RemoteFtpDirExists(uriStr, Info); if (b) { return(true); } MakeDir(uriStr, Info.FTPUserName, Info.FTPUserPwd); return(true); }
/// <summary> /// 上传文件到FTP服务器 /// </summary> /// <param name="ServiceIP">文件名以及详细路径</param> /// <param name="Info"></param> /// <param name="Paths"></param> /// <param name="FileUri"></param> /// <returns></returns> public static bool FtpUploadFile(string ServiceIP, FTPSeviceInfo Info, List <string> Paths, out string FileUri, Action <int, int> updateProgress = null) { string uri = "ftp://" + Info.FTPServiceIP + "/"; if (Paths != null && Paths.Count > 1) { Paths.ForEach(m => { if (m != null && m != string.Empty) { uri += m; uri += "/"; } }); } return(FtpUploadFile(new FileInfo(ServiceIP), Info.FTPUserName, Info.FTPUserPwd, uri, out FileUri, true)); }
/// <summary> /// 获取已上传文件大小 /// </summary> /// <param name="FileName">远程文件路径以及名称</param> /// <param name="path">服务器文件路径</param> /// <returns></returns> public static long GetFileSize(string Uri, FTPSeviceInfo Info) { long filesize = 0; try { FtpWebRequest reqFTP; reqFTP = (FtpWebRequest)FtpWebRequest.Create(Uri); reqFTP.KeepAlive = false; reqFTP.UseBinary = true; reqFTP.Credentials = new NetworkCredential(Info.FTPUserName, Info.FTPUserPwd);//用户,密码 reqFTP.Method = WebRequestMethods.Ftp.GetFileSize; FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse(); filesize = response.ContentLength; return(filesize); } catch { return(0); } }
/// <summary> /// 在ftp服务器上创建文件目录(批量) /// </summary> /// <param name="Paths">路径集合</param> /// <param name="Info">服务器信息</param> /// <returns></returns> public static bool MakeDir(List <string> Paths, FTPSeviceInfo Info) { var dirName = string.Empty; if (Paths != null && Paths.Count > 1) { Paths.ForEach(m => { if (m != null && m != string.Empty) { dirName += m; MakeDir(dirName, Info); dirName += "/"; } }); } else if (Paths.Count == 1) { MakeDir(Paths[0], Info); } return(true); }
/// <summary> /// 重命名 /// </summary> /// <param name="Info">FTP服务器信息</param> /// <param name="newFileName">新文件名</param> /// <param name="paths">路径集合</param> /// <returns></returns> public static bool FileRename(FTPSeviceInfo Info, string newFileName, List <string> paths) { string uri = string.Empty; var uriStr = "ftp://" + Info.FTPServiceIP + "/"; if (paths != null && paths.Count > 1) { paths.ForEach(m => { if (m != null && m != string.Empty) { uri += m; uri += "/"; } }); } else if (paths.Count == 1) { uri = paths[0]; } uriStr += uri; return(FileRename(uriStr, Info.FTPUserName, Info.FTPUserPwd, newFileName)); }
public FileUpLoadHelpDemo() { FTPInfo = new FTPSeviceInfo(); }
/// <summary> /// 删除文件 /// </summary> /// <param name="Uri">文件Uri</param> /// <param name="Info">服务器信息</param> /// <returns></returns> public static bool FileDelete(string Uri, FTPSeviceInfo Info) { return(FileDelete(Uri, Info.FTPUserName, Info.FTPUserPwd)); }
/// <summary> /// 重命名 /// </summary> /// <param name="Info">FTP服务器信息</param> /// <param name="newFileName">新文件名</param> /// <param name="path">文件在服务器上的路径</param> /// <returns></returns> public static bool FileRename(FTPSeviceInfo Info, string newFileName, string path) { return(FileRename(Info, newFileName, new List <string> { path })); }
/// <summary> /// 从FTP服务器下载文件,指定本地路径和本地文件名 /// </summary> /// <param name="remoteFileName">远程文件名</param> /// <param name="localFileName">保存本地的文件名(包含路径)</param> /// <param name="Info">服务器信息</param> /// <returns></returns> public static bool FtpDownload(string remoteFileName, string localFileName, FTPSeviceInfo Info) { return(FtpDownload(remoteFileName, localFileName, Info, true, false)); }
/// <summary> /// 初始化,并且设置FTP服务器IP /// </summary> /// <param name="FtpIp"></param> public FTPTool(string FtpIp) { FTPInfo = new FTPSeviceInfo { FTPServiceIP = FtpIp }; }
/// <summary> /// 从FTP服务器下载文件,指定本地路径和本地文件名(支持断点下载) /// </summary> /// <param name="remoteFileName">远程文件名</param> /// <param name="localFileName">保存本地的文件名(包含路径)</param> /// <param name="ifCredential">是否启用身份验证(false:表示允许用户匿名下载)</param> /// <param name="size">已下载文件流大小</param> /// <param name="updateProgress">报告进度的处理(第一个参数:总大小,第二个参数:当前进度)</param> /// <returns>是否下载成功</returns> public static bool FtpBrokenDownload(string remoteFileName, string localFileName, FTPSeviceInfo Info, bool ifCredential, long size, Action <int, int> updateProgress = null) { FtpWebRequest reqFTP; Stream ftpStream = null; FtpWebResponse response = null; FileStream outputStream = null; try { outputStream = new FileStream(localFileName, FileMode.Append); Uri uri = new Uri(remoteFileName); reqFTP = (FtpWebRequest)FtpWebRequest.Create(uri); reqFTP.Timeout = 15000; reqFTP.UseBinary = true; reqFTP.KeepAlive = false; reqFTP.ContentOffset = size; reqFTP.ReadWriteTimeout = 60000; if (ifCredential)//使用用户身份认证 { reqFTP.Credentials = new NetworkCredential(Info.FTPUserName, Info.FTPUserPwd); } long totalBytes = GetFileSize(remoteFileName, Info); reqFTP.Method = WebRequestMethods.Ftp.DownloadFile; response = (FtpWebResponse)reqFTP.GetResponse(); ftpStream = response.GetResponseStream(); //更新进度 if (updateProgress != null) { updateProgress((int)totalBytes, 0);//更新进度条 } long totalDownloadedByte = 0; int bufferSize = 2048; int readCount; byte[] buffer = new byte[bufferSize]; readCount = ftpStream.Read(buffer, 0, bufferSize); while (readCount > 0) { totalDownloadedByte = readCount + totalDownloadedByte; outputStream.Write(buffer, 0, readCount); //更新进度 if (updateProgress != null) { updateProgress((int)totalBytes, (int)totalDownloadedByte);//更新进度条 } readCount = ftpStream.Read(buffer, 0, bufferSize); } ftpStream.Close(); outputStream.Close(); response.Close(); return(true); } catch (Exception ex) { return(false); throw; } finally { if (ftpStream != null) { ftpStream.Close(); } if (outputStream != null) { outputStream.Close(); } if (response != null) { response.Close(); } } }
/// <summary> /// 初始化未设置FTP服务器 /// </summary> public FTPTool() { FTPInfo = new FTPSeviceInfo(); }
/// <summary> /// 判断ftp上的文件目录是否存在 /// </summary> /// <param name="uri"></param> /// <param name="Info">服务器信息</param> /// <returns></returns> public static bool RemoteFtpDirExists(string uri, FTPSeviceInfo Info) { return(RemoteFtpDirExists(uri, Info.FTPUserName, Info.FTPUserPwd)); }
/// <summary> /// 初始化,并且设置FTP服务器IP和用户名密码 /// </summary> /// <param name="FtpIp"></param> public FTPTool(string FtpIp, string FtpUserName, string FtpPwd) { FTPInfo = new FTPSeviceInfo { FTPServiceIP = FtpIp, FTPUserName = FtpUserName, FTPUserPwd = FtpPwd }; }
/// <summary> /// 重命名 /// </summary> /// <param name="uri">文件Uri</param> /// <param name="newFileName">新文件名</param> /// <param name="Info">服务器信息</param> /// <returns></returns> public static bool FileRename(string uri, string newFileName, FTPSeviceInfo Info) { return(FileRename(uri, Info.FTPUserName, Info.FTPUserPwd, newFileName)); }