⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ftpclient.cpp

📁 自己改写的在WINCE上开发用的EVC++的FTP操作示例工程,希望能给相关人士提供帮助.
💻 CPP
📖 第 1 页 / 共 4 页
字号:
	switch( representation.Type().AsEnum() )
	{
	case CType::tyASCII:     strCmd = "TYPE A";                             break;
	case CType::tyEBCDIC:    strCmd = "TYPE E";                             break;
	case CType::tyImage:     strCmd = "TYPE I";                             break;
	case CType::tyLocalByte: strCmd = (CMakeString() << "TYPE L " << dwSize).GetString(); break;
	default:
		ASSERT( false );
		return FTP_ERROR;
	}
	
	if( representation.Type()==CType::ASCII() || 
		representation.Type()==CType::EBCDIC() )
	{
		switch( representation.Format().AsEnum() )
		{
		case CTypeFormat::tfNonPrint:        strCmd += " N"; break;
		case CTypeFormat::tfTelnetFormat:    strCmd += " T"; break;
		case CTypeFormat::tfCarriageControl: strCmd += " C"; break;
		default:
			ASSERT( false );
		}
	}
	
	CReply Reply;
	if( !SendCommand(strCmd, Reply) )
		return FTP_ERROR;
	return SimpleErrorCheck(Reply);
}

/// Executes the ftp command CWD (CHANGE WORKING DIRECTORY)
/// This command allows the user to work with a different directory or dataset 
/// for file storage or retrieval without altering his login or accounting 
/// information. Transfer parameters are similarly unchanged.
/// @param[in] strDirectory Pathname specifying a directory or other system 
///                         dependent file group designator.
/// @return see return values of CFTPClient::SimpleErrorCheck
int CFTPClient::ChangeWorkingDirectory(CStringA strDirectory)
{
	ASSERT( !strDirectory.IsEmpty() );
	CStringA strCmd;
	strCmd = (CMakeString() << "CWD " << strDirectory).GetString();
	
	CReply Reply;
	if( !SendCommand(strCmd, Reply) )
		return FTP_ERROR;
	return SimpleErrorCheck(Reply);
}

/// Executes the ftp command MKD (MAKE DIRECTORY)
/// This command causes the directory specified in the pathname to be created 
/// as a directory (if the pathname is absolute) or as a subdirectory of the 
/// current working directory (if the pathname is relative).
/// @pararm[in] strDirectory Pathname specifying a directory to be created.
/// @return see return values of CFTPClient::SimpleErrorCheck
int CFTPClient::MakeDirectory(CStringA& strDirectory)
{
	ASSERT( !strDirectory.IsEmpty() );
	CStringA strCmd;
	strCmd = (CMakeString() << "MKD " << strDirectory).GetString();
	
	CReply Reply;
	if( !SendCommand(strCmd, Reply) )
		return FTP_ERROR;
	return SimpleErrorCheck(Reply);
}

/// Executes the ftp command SITE (SITE PARAMETERS)
/// This command is used by the server to provide services specific to his 
/// system that are essential to file transfer but not sufficiently universal 
/// to be included as commands in the protocol.  The nature of these services 
/// and the specification of their syntax can be stated in a reply to the HELP 
/// SITE command.
/// @param[in] strCmd Command to be executed.
/// @return see return values of CFTPClient::SimpleErrorCheck
int CFTPClient::SiteParameters(CStringA& strCmd)
{
	ASSERT( !strCmd.IsEmpty() );
	CStringA strCmd2;
	strCmd2 = (CMakeString() << "SITE " << strCmd).GetString();
	
	CReply Reply;
	if( !SendCommand(strCmd2, Reply) )
		return FTP_ERROR;
	return SimpleErrorCheck(Reply);
}

/// Executes the ftp command HELP
/// This command shall cause the server to send helpful information regarding 
/// its implementation status over the control connection to the user.
/// The command may take an argument (e.g., any command name) and return more 
/// specific information as a response.  The reply is type 211 or 214.
/// It is suggested that HELP be allowed before entering a USER command. The 
/// server may use this reply to specify site-dependent parameters, e.g., in 
/// response to HELP SITE.
/// @param[in] strTopic Topic of the requested help.
/// @return see return values of CFTPClient::SimpleErrorCheck
int CFTPClient::Help(CStringA& strTopic)
{
	CStringA strCmd;
	strCmd = (CMakeString() << (strTopic.IsEmpty() ? "HELP":"HELP ") << strTopic).GetString();
	
	CReply Reply;
	if( !SendCommand(strCmd, Reply) )
		return FTP_ERROR;
	return SimpleErrorCheck(Reply);
}

/// Executes the ftp command DELE (DELETE)
/// This command causes the file specified in the pathname to be deleted at the 
/// server site.  If an extra level of protection is desired (such as the query, 
/// "Do you really wish to delete?"), it should be provided by the user-FTP process.
/// @param[in] strFile Pathname of the file to delete.
/// @return see return values of CFTPClient::SimpleErrorCheck
int CFTPClient::Delete(CStringA& strFile)
{
	ASSERT( !strFile.IsEmpty() );
	CStringA strCmd;
	strCmd = (CMakeString() << "DELE " << strFile).GetString();
	
	CReply Reply;
	if( !SendCommand(strCmd, Reply) )
		return FTP_ERROR;
	return SimpleErrorCheck(Reply);
}

/// Executes the ftp command RMD (REMOVE DIRECTORY)
/// This command causes the directory specified in the pathname to be removed 
/// as a directory (if the pathname is absolute) or as a subdirectory of the 
/// current working directory (if the pathname is relative).
/// @param[in] strDirectory Pathname of the directory to delete.
/// @return see return values of CFTPClient::SimpleErrorCheck
int CFTPClient::RemoveDirectory(CStringA& strDirectory)
{
	ASSERT( !strDirectory.IsEmpty() );
	
	CStringA strCmd;
	strCmd = (CMakeString() << "RMD " << strDirectory).GetString();
	
	CReply Reply;
	if( !SendCommand(strCmd, Reply) )
		return FTP_ERROR;
	return SimpleErrorCheck(Reply);
}

/// Executes the ftp command STRU (FILE STRUCTURE)
/// see documentation of nsFTP::CStructure
/// The default structure is File.
/// @param[in] crStructure see Documentation of nsFTP::CStructure
/// @return see return values of CFTPClient::SimpleErrorCheck
int CFTPClient::FileStructure(CStructure& crStructure)
{
	CStringA strStructure;
	switch( crStructure.AsEnum() )
	{
	case CStructure::scFile:   strStructure="F"; break;
	case CStructure::scRecord: strStructure="R"; break;
	case CStructure::scPage:   strStructure="P"; break;
	default:
		ASSERT( false );
		return FTP_ERROR;
	}
	
	CStringA strCmd;
	strCmd = (CMakeString() << "STRU " << strStructure).GetString();
	
	CReply Reply;
	if( !SendCommand(strCmd, Reply) )
		return FTP_ERROR;
	return SimpleErrorCheck(Reply);
}

/// Executes the ftp command MODE (TRANSFER MODE)
/// see documentation of nsFTP::CTransferMode
/// The default transfer mode is Stream.
/// @param[in] crTransferMode see Documentation of nsFTP::CTransferMode
/// @return see return values of CFTPClient::SimpleErrorCheck
int CFTPClient::TransferMode(CTransferMode& crTransferMode)
{
	CStringA strMode;
	switch( crTransferMode.AsEnum() )
	{
	case CTransferMode::tmStream:      strMode="S"; break;
	case CTransferMode::tmBlock:       strMode="B"; break;
	case CTransferMode::tmCompressed:  strMode="C"; break;
	default:
		ASSERT( false );
		return FTP_ERROR;
	}
	
	CStringA strCmd;
	strCmd = (CMakeString() << "MODE " << strMode).GetString();
	
	CReply Reply;
	if( !SendCommand(strCmd, Reply) )
		return FTP_ERROR;
	return SimpleErrorCheck(Reply);
}

/// Executes the ftp command STAT (STATUS)
/// This command shall cause a status response to be sent over the control 
/// connection in the form of a reply. The command may be sent during a file 
/// transfer (along with the Telnet IP and Synch signals--see the Section on 
/// FTP Commands) in which case the server will respond with the status of the
/// operation in progress, or it may be sent between file transfers. In the 
/// latter case, the command may have an argument field. 
/// @param[in] strPath If the argument is a pathname, the command is analogous 
///                    to the "list" command except that data shall be transferred 
///                    over the control connection. If a partial pathname is 
///                    given, the server may respond with a list of file names or 
///                    attributes associated with that specification. If no argument 
///                    is given, the server should return general status information 
///                    about the server FTP process. This should include current 
///                    values of all transfer parameters and the status of connections.
/// @return see return values of CFTPClient::SimpleErrorCheck
int CFTPClient::Status(CStringA& strPath)
{
	CStringA strCmd;
	strCmd = (CMakeString() << (strPath.IsEmpty() ? "STAT":"STAT ") << strPath).GetString();
	
	CReply Reply;
	if( !SendCommand(strCmd, Reply) )
		return FTP_ERROR;
	return SimpleErrorCheck(Reply);
}

/// Executes the ftp command ALLO (ALLOCATE)
/// This command may be required by some servers to reserve sufficient storage 
/// to accommodate the new file to be transferred.
/// @param[in] iReserveBytes The argument shall be a decimal integer representing 
///                          the number of bytes (using the logical byte size) of 
///                          storage to be reserved for the file. For files sent 
///                          with record or page structure a maximum record or page
///                          size (in logical bytes) might also be necessary; this 
///                          is indicated by a decimal integer in a second argument 
///                          field of the command.
/// @pararm[in] piMaxPageOrRecordSize This second argument is optional. This command 
///                          shall be followed by a STORe or APPEnd command.
///                          The ALLO command should be treated as a NOOP (no operation)
///                          by those servers which do not require that the maximum 
///                          size of the file be declared beforehand, and those servers 
///                          interested in only the maximum record or page size should 
///                          accept a dummy value in the first argument and ignore it.
/// @return see return values of CFTPClient::SimpleErrorCheck
int CFTPClient::Allocate(int iReserveBytes, int* piMaxPageOrRecordSize/*=NULL*/)
{
	CStringA strCmd;
	if( piMaxPageOrRecordSize==NULL )
		strCmd = (CMakeString() << "ALLO " << iReserveBytes).GetString();
	else
		strCmd = (CMakeString() << "ALLO " << iReserveBytes << " R " << *piMaxPageOrRecordSize).GetString();
	
	CReply Reply;
	if( !SendCommand(strCmd, Reply) )
		return FTP_ERROR;
	return SimpleErrorCheck(Reply);
}

/// Executes the ftp command SMNT ()
/// @return see return values of CFTPClient::SimpleErrorCheck
int CFTPClient::StructureMount(CStringA& strPath)
{
	CStringA strCmd;
	strCmd = (CMakeString() << "SMNT " << strPath).GetString();
	
	CReply Reply;
	if( !SendCommand(strCmd, Reply) )
		return FTP_ERROR;
	return SimpleErrorCheck(Reply);
}

/// Executes the ftp command (STRUCTURE MOUNT)
/// This command allows the user to mount a different file system data structure 
/// without altering his login or accounting information. Transfer parameters 
/// are similarly unchanged.  The argument is a pathname specifying a directory 
/// or other system dependent file group designator.
/// @return see return values of CFTPClient::SimpleErrorCheck
int CFTPClient::Reinitialize()
{
	CStringA strCmd;
	strCmd = (CMakeString() << "REIN").GetString();
	
	CReply Reply;
	if( !SendCommand(strCmd, Reply) )
		return FTP_ERROR;
	
	if( Reply.Code().IsPositiveCompletionReply() )
		return FTP_OK;
	else if( Reply.Code().IsPositivePreliminaryReply() )
	{
		if( !GetResponse(Reply) || !Reply.Code().IsPositiveCompletionReply() )
			return FTP_ERROR;
	}
	else if( Reply.Code().IsNegativeReply() )
		return FTP_NOTOK;
	
	ASSERT( Reply.Code().IsPositiveIntermediateReply() );
	return FTP_ERROR;
}

/// Executes the ftp command REST (RESTART)
/// This command does not cause file transfer but skips over the file to the 
/// specified data checkpoint. This command shall be immediately followed
/// by the appropriate FTP service command which shall cause file transfer 
/// to resume.
/// @param[in] dwPosition Represents the server marker at which file transfer 
///                       is to be restarted.
/// @return see return values of CFTPClient::SimpleErrorCheck
int CFTPClient::Restart(DWORD dwPosition)
{
	CStringA strCmd;
	strCmd = (CMakeString() << "REST " << dwPosition).GetString();
	
	CReply Reply;
	if( !SendCommand(strCmd, Reply) )
		return FTP_ERROR;
	
	if( Reply.Code().IsPositiveIntermediateReply() )
		return FTP_OK;
	else if( Reply.Code().IsNegativeReply() )
		return FTP_NOTOK;
	
	ASSERT( Reply.Code().IsPositiveReply() );
	
	return FTP_ERROR;
}

/// Executes the ftp command SIZE
/// Return size of file.
/// SIZE is not specified in RFC 959.
/// @param[in] Pathname of a file.
/// @param[out] Size of the file specified in pathname.
/// @return see return values of CFTPClient::SimpleErrorCheck
int CFTPClient::FileSize(CStringA& strPath, long& lSize)
{
	CReply Reply;
	CStringA strCmd = "SIZE ";
	strCmd += strPath;
	if( !SendCommand(strCmd, Reply) )
		return FTP_ERROR;
	lSize = StringToLong(Reply.Value().Mid(4));
	return SimpleErrorCheck(Reply);
}

/// Executes the ftp command MDTM
/// Show last modification time of file.
/// MDTM is not specified in RFC 959.
/// @param[in] strPath Pathname of a file.
/// @param[out] strModificationTime Modification time of the file specified in pathname.
/// @return see return values of CFTPClient::SimpleErrorCheck
int CFTPClient::FileModificationTime(CStringA& strPath, CStringA& strModificationTime)
{
	strModificationTime = "";
	
	CReply Reply;
	CStringA strCmd = "MDTM ";
	strCmd += strPath;
	if( !SendCommand(strCmd, Reply) )
		return FTP_ERROR;
	
	if( Reply.Value().GetLength()>=18 )
	{
		CStringA strTemp(Reply.Value().Mid(4));
		size_t iPos=strTemp.Find('.');
		if( iPos>-1 )
			strTemp = strTemp.Mid(0, iPos);
		if( strTemp.GetLength()==14 )
			strModificationTime=strTemp;
	}
	
	if( strModificationTime.IsEmpty() )
		return FTP_ERROR;
	
	return SimpleErrorCheck(Reply);
}

/// Show last modification time of file.
/// @param[in] strPath Pathname of a file.
/// @param[out] tmModificationTime Modification time of the file specified in pathname.
/// @return see return values of CFTPClient::SimpleErrorCheck
int CFTPClient::FileModificationTime(CStringA& strPath, tm& tmModificationTime)
{
	CStringA strTemp;
	int iRet = FileModificationTime(strPath, strTemp);
	
	memset(&tmModificationTime, 0, sizeof(tmModificationTime));
	if( iRet==FTP_OK )
	{
		tmModificationTime.tm_year = StringToLong(strTemp.Mid(0, 4));
		tmModificationTime.tm_mon  = StringToLong(strTemp.Mid(4, 2));
		tmModificationTime.tm_mday = StringToLong(strTemp.Mid(6, 2));
		tmModificationTime.tm_hour = StringToLong(strTemp.Mid(8, 2));
		tmModificationTime.tm_min  = StringToLong(strTemp.Mid(10, 2));
		tmModificationTime.tm_sec  = StringToLong(strTemp.Mid(12));
	}
	return iRet;
}

/// Notifies all observers that an error occurred.
/// @param[in] strErrorMsg Error message which is reported to all observers.
/// @param[in] Name of the sourcecode file where the error occurred.
/// @param[in] Line number in th sourcecode file where the error occurred.
void CFTPClient::ReportError(CStringA strErrorMsg, CStringA strFile, DWORD dwLineNr)
{
	if(m_pNotification)
		m_pNotification->OnInternalError(strErrorMsg, strFile, dwLineNr);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -