public FTPDataConnection(IFTPConnection conn) { _conn = conn; _transType = conn.TransferType; set_transfer_type(conn, _transType); _conn.Lock(); set_up_data_connection(conn); }
/// <summary> /// Retrieves a remote file /// </summary> /// <param name="remoteFileName">The remote filename</param> /// <param name="localFileName">The local filename</param> /// <param name="type">The transfer type</param> public override void GetFile(string remoteFileName, string localFileName, FTPFileTransferType type) { var ftStruct = new FileTransferStruct { LocalFileName = localFileName, RemoteFileName = remoteFileName, Type = type }; _getFileTransfersQueue.Enqueue(ftStruct); ThreadPool.QueueUserWorkItem(GetFileFromQueue); }
private void SetTransferType(FTPFileTransferType type) { switch (type) { case FTPFileTransferType.ASCII: SetMode("TYPE A"); break; case FTPFileTransferType.Binary: SetMode("TYPE I"); break; default: throw new Exception("Invalid File Transfer Type"); } }
/// <summary> /// 下载断点续传 /// </summary> /// <param name="remoteFileName"></param> /// <param name="stream"></param> /// <param name="type"></param> public void GetStreamContinue(string remoteFileName, Stream stream, FTPFileTransferType type) { TcpListener listner = null; TcpClient client = null; NetworkStream networkStream = null; ArrayList tempMessageList = null; int returnValue = 0; //Begin 增加断点续传功能 发送REST命令给FTP服务器 tempMessageList = new ArrayList(); tempMessageList = SendCommand("REST "+stream.Length); returnValue = GetMessageReturnValue((string)tempMessageList[0]); if (returnValue != 350) { throw new Exception((string)tempMessageList[0]); } //End 增加断点续传功能 发送REST命令给FTP服务器 tempMessageList = new ArrayList(); string returnValueMessage = ""; LockTcpClient(); SetTransferType(type); if (this.mode == FTPMode.Active) { listner = CreateDataListner(); listner.Start(); } else { client = CreateDataClient(); } tempMessageList = new ArrayList(); tempMessageList = SendCommand("RETR " + remoteFileName); returnValue = GetMessageReturnValue((string)tempMessageList[0]); if (!(returnValue == 150 || returnValue == 125)) { throw new Exception((string)tempMessageList[0]); } if (this.mode == FTPMode.Active) { client = listner.AcceptTcpClient(); } networkStream = client.GetStream(); Byte[] buffer = new Byte[BLOCK_SIZE]; int bytes = 0; bool read = true; while (read) { bytes = (int)networkStream.Read(buffer, 0, buffer.Length); stream.Write(buffer, 0, bytes); if (bytes == 0) { read = false; } } networkStream.Close(); client.Close(); if (this.mode == FTPMode.Active) { listner.Stop(); } if (tempMessageList.Count == 1) { tempMessageList = Read(); returnValue = GetMessageReturnValue((string)tempMessageList[0]); returnValueMessage = (string)tempMessageList[0]; } else { returnValue = GetMessageReturnValue((string)tempMessageList[1]); returnValueMessage = (string)tempMessageList[1]; } if (!(returnValue == 226)) { throw new Exception(returnValueMessage); } UnlockTcpClient(); }
public override void GetFile(string remoteFileName, string localFileName, FTPFileTransferType type) { EnqueueThread(CreateGetFileThread(remoteFileName, localFileName, type)); }
public override void GetFile(string remoteFileName, string localFileName, FTPFileTransferType type) { enqueue_thread(create_get_file_thread(remoteFileName, localFileName, type)); }
private int set_transfer_type(IFTPConnection conn, FTPFileTransferType type) { int result = 0; switch (type) { case FTPFileTransferType.ASCII: result = set_mode(conn, "TYPE A"); break; case FTPFileTransferType.Binary: result = set_mode(conn, "TYPE I"); break; default: _currentMsg = "Invalid File Transfer Type"; break; } return result; }
public override void SendFile(string localFileName, string remoteFileName, FTPFileTransferType type) { EnqueueThread(CreateSendFileThread(localFileName, remoteFileName, type)); }
/// <summary> /// Sends a stream to a remote file /// </summary> /// <param name="stream">The stream to send</param> /// <param name="remoteFileName">The remote file name</param> /// <param name="type">The transfer type</param> public void SendStream(Stream stream, string remoteFileName, FTPFileTransferType type) { TcpListener listener = null; TcpClient client = null; NetworkStream networkStream = null; var tempMessageList = new List <string>(); int returnValue = 0; string returnValueMessage = ""; lock (_tcpClient) { SetTransferType(type); if (_mode == FTPMode.Active) { listener = CreateDataListener(); listener.Start(); } else { client = CreateDataClient(); } tempMessageList = SendCommand("STOR " + remoteFileName); returnValue = GetMessageReturnValue(tempMessageList[0]); if (!(returnValue == 150 || returnValue == 125)) { throw new Exception(tempMessageList[0]); } if (_mode == FTPMode.Active) { client = listener.AcceptTcpClient(); } networkStream = client.GetStream(); var buf = new byte[BlockSize]; int bytesRead = 0; int totalBytes = 0; while (totalBytes < stream.Length) { bytesRead = stream.Read(buf, 0, BlockSize); totalBytes = totalBytes + bytesRead; networkStream.Write(buf, 0, bytesRead); } stream.Close(); networkStream.Close(); client.Close(); if (_mode == FTPMode.Active) { listener.Stop(); } if (tempMessageList.Count == 1) { tempMessageList = Read(); returnValue = GetMessageReturnValue(tempMessageList[0]); returnValueMessage = tempMessageList[0]; } else { returnValue = GetMessageReturnValue(tempMessageList[1]); returnValueMessage = tempMessageList[1]; } if (!(returnValue == 226)) { throw new Exception(returnValueMessage); } } }
/// <summary> /// Connects a stream to remote file /// </summary> /// <param name="remoteFileName">The remote file name</param> /// <param name="stream">The stream to connect to the remote file</param> /// <param name="type">The transfer type</param> public void GetStream(string remoteFileName, Stream stream, FTPFileTransferType type) { TcpListener listener = null; TcpClient client = null; NetworkStream networkStream = null; List<string> tempMessageList; int returnValue = 0; string returnValueMessage = ""; lock (_tcpClient) { SetTransferType(type); if (_mode == FTPMode.Active) { listener = CreateDataListener(); listener.Start(); } else { client = CreateDataClient(); } tempMessageList = SendCommand("RETR " + remoteFileName); returnValue = GetMessageReturnValue(tempMessageList[0]); if (!(returnValue == 150 || returnValue == 125)) { throw new Exception(tempMessageList[0]); } if (_mode == FTPMode.Active) { client = listener.AcceptTcpClient(); } networkStream = client.GetStream(); var buffer = new byte[BlockSize]; int bytesRead = 0; bool bRead = true; while (bRead) { bytesRead = networkStream.Read(buffer, 0, buffer.Length); stream.Write(buffer, 0, bytesRead); if (bytesRead == 0) { bRead = false; } } stream.Close(); networkStream.Close(); client.Close(); if (_mode == FTPMode.Active) { listener.Stop(); } if (tempMessageList.Count == 1) { tempMessageList = Read(); returnValue = GetMessageReturnValue(tempMessageList[0]); returnValueMessage = tempMessageList[0]; } else { returnValue = GetMessageReturnValue(tempMessageList[1]); returnValueMessage = tempMessageList[1]; } if (!(returnValue == 226)) { throw new Exception(returnValueMessage); } } }
public override void GetFile(string remoteFileName, FTPFileTransferType type) { GetFile(remoteFileName, Path.GetFileName(remoteFileName), type); }
/// <summary> /// Sends a stream to a remote file /// </summary> /// <param name="stream">The stream to send</param> /// <param name="remoteFileName">The remote file name</param> /// <param name="type">The transfer type</param> public void SendStream(Stream stream, string remoteFileName, FTPFileTransferType type) { TcpListener listener = null; TcpClient client = null; NetworkStream networkStream = null; var tempMessageList = new List<string>(); int returnValue = 0; string returnValueMessage = ""; lock (_tcpClient) { SetTransferType(type); if (_mode == FTPMode.Active) { listener = CreateDataListener(); listener.Start(); } else { client = CreateDataClient(); } tempMessageList = SendCommand("STOR " + remoteFileName); returnValue = GetMessageReturnValue(tempMessageList[0]); if (!(returnValue == 150 || returnValue == 125)) { throw new Exception(tempMessageList[0]); } if (_mode == FTPMode.Active) { client = listener.AcceptTcpClient(); } networkStream = client.GetStream(); var buf = new byte[BlockSize]; int bytesRead = 0; int totalBytes = 0; while (totalBytes < stream.Length) { bytesRead = stream.Read(buf, 0, BlockSize); totalBytes = totalBytes + bytesRead; networkStream.Write(buf, 0, bytesRead); } stream.Close(); networkStream.Close(); client.Close(); if (_mode == FTPMode.Active) { listener.Stop(); } if (tempMessageList.Count == 1) { tempMessageList = Read(); returnValue = GetMessageReturnValue(tempMessageList[0]); returnValueMessage = tempMessageList[0]; } else { returnValue = GetMessageReturnValue(tempMessageList[1]); returnValueMessage = tempMessageList[1]; } if (!(returnValue == 226)) { throw new Exception(returnValueMessage); } } }
/// <summary> /// Sends a file to the remote server /// </summary> /// <param name="localFileName">The local filename</param> /// <param name="remoteFileName">The remote filename</param> /// <param name="type">The transfer type</param> public virtual void SendFile(string localFileName, string remoteFileName, FTPFileTransferType type) { using (var file = new FileStream(localFileName, FileMode.Open)) { SendStream(file, remoteFileName, type); } }
/// <summary> /// Retrieves a remote file /// </summary> /// <param name="remoteFileName">The remote file name</param> /// <param name="localFileName">The local file name</param> /// <param name="type">The transfer type</param> public virtual void GetFile(string remoteFileName, string localFileName, FTPFileTransferType type) { GetStream(remoteFileName, new FileStream(localFileName, FileMode.Create), type); }
/// <summary> /// Retrieves a remote file /// </summary> /// <param name="remoteFileName">The remote file name</param> /// <param name="type">The transfer type</param> public virtual void GetFile(string remoteFileName, FTPFileTransferType type) { GetFile(remoteFileName, Path.GetFileName(remoteFileName), type); }
/// <summary> /// Connects a stream to remote file /// </summary> /// <param name="remoteFileName">The remote file name</param> /// <param name="stream">The stream to connect to the remote file</param> /// <param name="type">The transfer type</param> public void GetStream(string remoteFileName, Stream stream, FTPFileTransferType type) { TcpListener listener = null; TcpClient client = null; NetworkStream networkStream = null; List <string> tempMessageList; int returnValue = 0; string returnValueMessage = ""; lock (_tcpClient) { SetTransferType(type); if (_mode == FTPMode.Active) { listener = CreateDataListener(); listener.Start(); } else { client = CreateDataClient(); } tempMessageList = SendCommand("RETR " + remoteFileName); returnValue = GetMessageReturnValue(tempMessageList[0]); if (!(returnValue == 150 || returnValue == 125)) { throw new Exception(tempMessageList[0]); } if (_mode == FTPMode.Active) { client = listener.AcceptTcpClient(); } networkStream = client.GetStream(); var buffer = new byte[BlockSize]; int bytesRead = 0; bool bRead = true; while (bRead) { bytesRead = networkStream.Read(buffer, 0, buffer.Length); stream.Write(buffer, 0, bytesRead); if (bytesRead == 0) { bRead = false; } } stream.Close(); networkStream.Close(); client.Close(); if (_mode == FTPMode.Active) { listener.Stop(); } if (tempMessageList.Count == 1) { tempMessageList = Read(); returnValue = GetMessageReturnValue(tempMessageList[0]); returnValueMessage = tempMessageList[0]; } else { returnValue = GetMessageReturnValue(tempMessageList[1]); returnValueMessage = tempMessageList[1]; } if (!(returnValue == 226)) { throw new Exception(returnValueMessage); } } }
/// <summary> /// 上传断点续传 /// </summary> /// <param name="localFileName"></param> /// <param name="remoteFileName"></param> /// <param name="type"></param> public virtual void SendFileContinue(string localFileName, string remoteFileName, FTPFileTransferType type) { FileStream fs = new FileStream(localFileName, FileMode.Open); SendStream(fs, remoteFileName, type); fs.Close(); }
/// <summary> /// 上传 断点续传 /// </summary> /// <param name="stream"></param> /// <param name="remoteFileName"></param> /// <param name="type"></param> public void SendStreamContinue(Stream stream, string remoteFileName, FTPFileTransferType type) { LockTcpClient(); TcpListener listner = null; TcpClient client = null; NetworkStream networkStream = null; ArrayList tempMessageList = new ArrayList(); int returnValue = 0; string returnValueMessage = ""; tempMessageList = new ArrayList(); SetTransferType(type); if (this.mode == FTPMode.Active) { listner = CreateDataListner(); listner.Start(); } else { client = CreateDataClient(); } #region 断点续传 //需要修改 long startPosition = 0; //Send REST 命令 tempMessageList = SendCommand("REST " + startPosition); returnValue = GetMessageReturnValue((string)tempMessageList[0]); if (returnValue != 350) { throw new Exception((string)tempMessageList[0]); } tempMessageList = SendCommand("APPE " + remoteFileName); returnValue = GetMessageReturnValue((string)tempMessageList[0]); if (!(returnValue == 150 || returnValue == 125)) { throw new Exception((string)tempMessageList[0]); } #endregion //tempMessageList = SendCommand("STOR " + remoteFileName); //returnValue = GetMessageReturnValue((string)tempMessageList[0]); //if (!(returnValue == 150 || returnValue == 125)) //{ // throw new Exception((string)tempMessageList[0]); //} if (this.mode == FTPMode.Active) { client = listner.AcceptTcpClient(); } networkStream = client.GetStream(); Byte[] buffer = new Byte[BLOCK_SIZE]; int bytes = 0; int totalBytes = 0; while (totalBytes < stream.Length) { bytes = (int)stream.Read(buffer, 0, BLOCK_SIZE); totalBytes = totalBytes + bytes; networkStream.Write(buffer, 0, bytes); } networkStream.Close(); client.Close(); if (this.mode == FTPMode.Active) { listner.Stop(); } if (tempMessageList.Count == 1) { tempMessageList = Read(); returnValue = GetMessageReturnValue((string)tempMessageList[0]); returnValueMessage = (string)tempMessageList[0]; } else { returnValue = GetMessageReturnValue((string)tempMessageList[1]); returnValueMessage = (string)tempMessageList[1]; } if (!(returnValue == 226)) { throw new Exception(returnValueMessage); } UnlockTcpClient(); }
public override void SendFile(string localFileName, string remoteFileName, FTPFileTransferType type) { enqueue_thread(create_send_file_thread(localFileName, remoteFileName, type)); }
public override void SendFile(string localFileName, FTPFileTransferType type) { SendFile(localFileName, Path.GetFileName(localFileName), type); }
private Thread create_send_file_thread(string localFileName, string remoteFileName, FTPFileTransferType type) { var ft = new FileTransferStruct {LocalFileName = localFileName, RemoteFileName = remoteFileName, Type = type}; this.sendFileTransfersQueue.Enqueue(ft); var thread = new Thread(new ThreadStart(SendFileFromQueue)) { Name = "GetFileFromQueue " + localFileName + ", " + remoteFileName + ", " + type.ToString() }; ; return thread; }
//------------------------Private methods: ------------------------------------------------- private Thread CreateGetFileThread(string remoteFileName, string localFileName, FTPFileTransferType type) { FileTransferStruct ft = new FileTransferStruct(); ft.LocalFileName = localFileName; ft.RemoteFileName = remoteFileName; ft.Type = type; this.getFileTransfersQueue.Enqueue(ft); Thread thread = new Thread(new ThreadStart(GetFileFromQueue)); thread.Name = "GetFileFromQueue " + remoteFileName + ", " + localFileName + ", " + type.ToString();; return(thread); }
public void GetStream(string remoteFileName, Stream stream, FTPFileTransferType type) { TcpListener listner = null; TcpClient client = null; NetworkStream networkStream = null; ArrayList tempMessageList = new ArrayList(); int returnValue = 0; string returnValueMessage = ""; LockTcpClient(); SetTransferType(type); if (this.mode == FTPMode.Active) { listner = CreateDataListner(); listner.Start(); } else { client = CreateDataClient(); } tempMessageList = new ArrayList(); tempMessageList = SendCommand("RETR " + remoteFileName); returnValue = GetMessageReturnValue((string)tempMessageList[0]); if (!(returnValue == 150 || returnValue == 125)) { throw new Exception((string)tempMessageList[0]); } if (this.mode == FTPMode.Active) { client = listner.AcceptTcpClient(); } networkStream = client.GetStream(); Byte[] buffer = new Byte[BLOCK_SIZE]; int bytes = 0; bool read = true; while (read) { bytes = (int)networkStream.Read(buffer, 0, buffer.Length); stream.Write(buffer, 0, bytes); if (bytes == 0) { read = false; } } networkStream.Close(); client.Close(); if (this.mode == FTPMode.Active) { listner.Stop(); } if (tempMessageList.Count == 1) { tempMessageList = Read(); returnValue = GetMessageReturnValue((string)tempMessageList[0]); returnValueMessage = (string)tempMessageList[0]; } else { returnValue = GetMessageReturnValue((string)tempMessageList[1]); returnValueMessage = (string)tempMessageList[1]; } if (!(returnValue == 226)) { throw new Exception(returnValueMessage); } UnlockTcpClient(); }
private Thread create_get_file_thread(string remoteFileName, string localFileName, FTPFileTransferType type) { FileTransferStruct ft = new FileTransferStruct(); ft.LocalFileName = localFileName; ft.RemoteFileName = remoteFileName; ft.Type = type; this.getFileTransfersQueue.Enqueue(ft); Thread thread = new Thread(new ThreadStart(get_file_from_queue)); thread.Name = "GetFileFromQueue " + remoteFileName + ", " + localFileName + ", " + type.ToString();; return thread; }
/// <summary> /// 设置传输类型 /// </summary> /// <param name="type"></param> private void SetTransferType(FTPFileTransferType type) { switch (type) { case FTPFileTransferType.ASCII: SetMode("TYPE A"); break; case FTPFileTransferType.Binary: SetMode("TYPE I"); break; default: throw new Exception("Invalid File Transfer Type"); } }
public virtual void GetFile(string remoteFileName, FTPFileTransferType type) { GetFile(remoteFileName, Path.GetFileName(remoteFileName), type); }
/// <summary> /// 下载断点续传 /// </summary> /// <param name="remoteFileName"></param> /// <param name="localFileName"></param> /// <param name="type"></param> public virtual void GetFileContinue(string remoteFileName, string localFileName, FTPFileTransferType type) { FileStream fs = new FileStream(localFileName, FileMode.Append); GetStream(remoteFileName, fs, type); fs.Close(); }