/// <summary> /// /// </summary> /// <param name="context"></param> /// <param name="filedownloadname"></param> /// <param name="dir"></param> /// <returns></returns> public string FtpStream(HttpContext context, string filedownloadname, string dir) { string stream = string.Empty; FtpConnection ftp = this.FtpConn(); ftp.SetCurrentDirectory(dir); //ftp. try { if (ftp.FileExist(filedownloadname)) { FtpStream ftpfs = ftp.OpenFile(filedownloadname, GenericRights.Read); stream = "<"; StreamReader reader = new StreamReader(ftpfs); while (reader.Read() > 0) { stream += reader.ReadToEnd(); } } else { context.Response.Write("<script>alert('file does not exist!');</script>"); } } finally { ftp.Close(); } return(stream); }
/* Download File */ public void Download(string localFile, string remoteFile) { try { /* Create an FTP Request */ FtpRequest = (FtpWebRequest)FtpWebRequest.Create(InterlocutorAddress + "/" + remoteFile); /* Log in to the FTP Server with the User Name and Password Provided */ FtpRequest.Credentials = new NetworkCredential(UserName, Password); LogInToInterlocutor(InterlocutorAddress); /* When in doubt, use these options */ FtpRequest.UseBinary = true; FtpRequest.UsePassive = true; FtpRequest.KeepAlive = true; /* Specify the Type of FTP Request */ FtpRequest.Method = WebRequestMethods.Ftp.DownloadFile; /* Establish Return Communication with the FTP Server */ FtpResponse = (FtpWebResponse)FtpRequest.GetResponse(); /* Get the FTP Server's Response Stream */ StartDownloadData(localFile, remoteFile, InterlocutorAddress); FtpStream = FtpResponse.GetResponseStream(); /* Open a File Stream to Write the Downloaded File */ FileStream localFileStream = new FileStream(localFile, FileMode.Create); /* Buffer for the Downloaded Data */ byte[] byteBuffer = new byte[BufferSize]; int bytesRead = FtpStream.Read(byteBuffer, 0, BufferSize); /* Download the File by Writing the Buffered Data Until the Transfer is Complete */ try { while (bytesRead > 0) { localFileStream.Write(byteBuffer, 0, bytesRead); bytesRead = FtpStream.Read(byteBuffer, 0, BufferSize); } } finally { /* Resource Cleanup */ localFileStream.Close(); EndDownloadData(localFile, remoteFile, InterlocutorAddress); } } finally { FtpStream.Close(); FtpResponse.Close(); FtpRequest = null; } }
/* Get the Size of a File */ public string GetFileSize(string fileName) { try { /* Create an FTP Request */ FtpRequest = (FtpWebRequest)FtpWebRequest.Create(InterlocutorAddress + "/" + fileName); /* Log in to the FTP Server with the User Name and Password Provided */ FtpRequest.Credentials = new NetworkCredential(UserName, Password); LogInToInterlocutor(InterlocutorAddress); /* When in doubt, use these options */ FtpRequest.UseBinary = true; FtpRequest.UsePassive = true; FtpRequest.KeepAlive = true; /* Specify the Type of FTP Request */ FtpRequest.Method = WebRequestMethods.Ftp.GetFileSize; /* Establish Return Communication with the FTP Server */ FtpResponse = (FtpWebResponse)FtpRequest.GetResponse(); /* Establish Return Communication with the FTP Server */ FtpStream = FtpResponse.GetResponseStream(); /* Get the FTP Server's Response Stream */ StreamReader ftpReader = new StreamReader(FtpStream); /* Store the Raw Response */ string fileInfo = null; /* Read the Full Response Stream */ try { while (ftpReader.Peek() != -1) { fileInfo = ftpReader.ReadToEnd(); } } finally { /* Resource Cleanup */ ftpReader.Close(); } /* Return File Size */ return(fileInfo); } finally { FtpStream.Close(); FtpResponse.Close(); FtpRequest = null; } }
/* List Directory Contents in Detail (Name, Size, Created, etc.) */ public string[] DirectoryListDetailed(string directory) { try { /* Create an FTP Request */ FtpRequest = (FtpWebRequest)FtpWebRequest.Create(InterlocutorAddress + "/" + directory); /* Log in to the FTP Server with the User Name and Password Provided */ FtpRequest.Credentials = new NetworkCredential(UserName, Password); LogInToInterlocutor(InterlocutorAddress); /* When in doubt, use these options */ FtpRequest.UseBinary = true; FtpRequest.UsePassive = true; FtpRequest.KeepAlive = true; /* Specify the Type of FTP Request */ FtpRequest.Method = WebRequestMethods.Ftp.ListDirectoryDetails; /* Establish Return Communication with the FTP Server */ FtpResponse = (FtpWebResponse)FtpRequest.GetResponse(); /* Establish Return Communication with the FTP Server */ FtpStream = FtpResponse.GetResponseStream(); /* Get the FTP Server's Response Stream */ StreamReader ftpReader = new StreamReader(FtpStream); /* Store the Raw Response */ string directoryRaw = null; /* Read Each Line of the Response and Append a Pipe to Each Line for Easy Parsing */ try { while (ftpReader.Peek() != -1) { directoryRaw += ftpReader.ReadLine() + "|"; } } finally { /* Resource Cleanup */ ftpReader.Close(); } /* Return the Directory Listing as a string Array by Parsing 'directoryRaw' with the Delimiter you Append (I use | in This Example) */ return(directoryRaw.Split("|".ToCharArray())); } finally { FtpStream.Close(); FtpResponse.Close(); FtpRequest = null; } }
public System.IO.Stream ReadFile(FileOrDirectory src) { FtpClient ftp = null; Task task = null; try { ftp = FtpConnections.Open(IsSource, ref url); if (ftp.FileTransferType != TransferType.Binary) ftp.FileTransferType = TransferType.Binary; var file = new FtpStream(); file.Client = ftp; file.Path = Url.Path() + "/" + src.Name; file.Size = src.Size; file.File = src; file.Log = Log; if (!UseFXP) { task = Services.Tasks.Do(() => { using (file) { try { if (TransferProgress) { progress[file.Client] = new ProgressData { ElapsedTime = new TimeSpan(0), Path = file.Path, Size = file.Size }; file.Client.TransferProgress += ShowProgress; } file.Start = DateTime.Now; ftp.GetFile(file.File.Name, file, false); } catch (Exception ex) { file.Exception(ex); Sync.Failure(file.File, ex, file.Client); } finally { if (TransferProgress) { file.Client.TransferProgress -= ShowProgress; progress.Remove(file.Client); } } } }); } return file; } catch (Exception e) { Sync.Failure(src, e, ftp); } finally { if (task == null && ftp != null) FtpConnections.Pass(ftp); } return null; }
/// <summary> /// 通过Ftp拷贝外网的文件 /// </summary> /// <param name="Psa_attachname">文件的名称</param> /// <param name="Psa_file_Size">文件的字节数</param> /// <param name="AttachPath">存放文件的路径[~/AffAirDirInfo/20080202/082600043/]~为服务器的物理路径</param> /// <param name="oYearDim"></param> /// <returns>拷贝文件是否成功</returns> public bool CopyFile(string Psa_attachname, int Psa_file_Size, string AttachPath, string oYearDim) { bool isPass = false; if (Psa_attachname == string.Empty || Psa_file_Size < 0) { return(false); } try { using (FtpConnection ftp = this.FtpConn()) { byte[] bytes = new byte[Psa_file_Size]; //string oProcEventAttachPath = GetProcEventAttachPath(); string oProcEventAttachPath = oYearDim + "/"; string fileUrl = oProcEventAttachPath + Psa_attachname; //ftp.SetCurrentDirectory("/"); if (ftp.FileExist(fileUrl)) { try { //ftp.SetCurrentDirectory(oProcEventAttachPath); FtpStream ftpfs = ftp.OpenFile(fileUrl, GenericRights.Read); //FtpStream ftpfs=ftp.OpenFile(Psa_attachname,GenericRights.Read); Stream oRtream = (Stream)ftpfs; FileStream fs = new FileStream(AttachPath + Psa_attachname, FileMode.Create, FileAccess.Write); //int index = oRtream.Read(bytes,0,Psa_file_Size); int count = oRtream.Read(bytes, 0, 10240); // HttpWebRequest webRequest = (HttpWebRequest) WebRequest.Create("http://211.144.95.130/pdhb.synadminweb/AffAirDirInfo/"+fileUrl); // HttpWebResponse webResponse = (HttpWebResponse) webRequest.GetResponse(); // Stream oRtream = webResponse.GetResponseStream(); // //int index = oRtream.Read(bytes,0,Psa_file_Size); // FileStream fs = new FileStream(AttachPath + Psa_attachname, FileMode.Create, FileAccess.Write); // int count = oRtream.Read( bytes, 0, 1024 ); fs.Write(bytes, 0, count); while (count > 0) { // Dump the 256 characters on a string and display the string onto the console. count = oRtream.Read(bytes, 0, 10240); fs.Write(bytes, 0, count); } oRtream.Close(); // webResponse.Close(); fs.Flush(); fs.Close(); isPass = true; } catch (Exception ex) { throw new Exception("FTP读取文件失败原因:" + ex.Message); } } // else // { // throw new Exception("文件不存在!"); // } } } catch (Exception ex) { throw new Exception("保存文件信息失败!请查看Ftp连接是否成功!原因:" + ex.Message, ex); } finally { ftp.Close(); } return(isPass); }
private void SafeRelease(ref FtpStream obj) { if (obj != null) { try { obj.Close(); } catch { } obj = null; } }