18,356
社区成员
发帖
与我相关
我的任务
分享
CInternetSession session;
CFtpConnection *pFtp=session.GetFtpConnection(FTP服务器地址,用户名,密码);
CInternetFile *pFtpFile=pFtp->OpenFile(文件路径);CFile file;
int DownLength=0;//已经下载了的数据长度
if(目的目录下没有指定文件)//从头开始下载
{
file.Open(目的文件路径,CFile::modeCreate|CFile::modeWrite);
}
else//断点续传
{
file.Open(目的文件路径,CFile::modeWrite);
file.SeekToEnd();
DownLength=file.GetLength();
pFtpFile->Seek(DownLength,CFile::begin);
}
int SrcLength=pFtpFile->GetLength();
while(DownLength<SrcLength)
{
读源文件;
写目的文件;
}
if(!localFile.Open(m_strLocalFolder + vedioFileName ,CFile::modeWrite|CFile::modeNoTruncate|CFile::modeCreate|CFile::shareDenyNone))
return false;
//设置文件大小
CFileStatus fileStatus;
localFile.GetStatus(fileStatus);
m_nFileTransSize=fileStatus.m_size;
localFile.SeekToEnd();
CString pszOffset;
pszOffset.Format("%d",m_nFileTransSize);
HINTERNET hResponse;
CString strSeekOffsetCMD = "RETR ";
DWORD dwError;
TCHAR databuf1[200];
TCHAR databuf2[200];
TCHAR databuf3[200];
TCHAR databuf4[200];
TCHAR databuf5[200];
DWORD size=200;
pFtpCon->Command("TYPE I",CFtpConnection::CmdRespNone);
InternetGetLastResponseInfo(&dwError,databuf1,&size);
//这里的m_strFtpFilE是指 ftp://file1/file2.exe 中的"file1//file2.exe"
pFtpCon->Command("SIZE /"+m_strFtpFile,CFtpConnection::CmdRespNone);
InternetGetLastResponseInfo(&dwError,databuf1,&size);
pFtpCon->Command("PASV",CFtpConnection::CmdRespNone);
InternetGetLastResponseInfo(&dwError,databuf1,&size);
int dataPort=ParsePasv(databuf1);
pFtpCon->Command("PASV",CFtpConnection::CmdRespNone);
InternetGetLastResponseInfo(&dwError,databuf1,&size);
dataPort=ParsePasv(databuf1);
SOCKET recSocket;
sockaddr_in saServer;
hostent* remoteHost;
char* remoteIP;
// Create a listening socket
recSocket = socket(AF_INET, SOCK_STREAM, 0);
// Get the local host information
remoteHost = gethostbyname(m_ftpIp);
remoteIP = inet_ntoa (*(struct in_addr *)*remoteHost->h_addr_list);
// Set up the sockaddr structure
saServer.sin_family = AF_INET;
saServer.sin_addr.s_addr = inet_addr(remoteIP);
saServer.sin_port = htons(dataPort);
// Bind the listening socket using the
// information in the sockaddr structure
InternetGetLastResponseInfo(&dwError,databuf4,&size);
int ret = connect( recSocket, (SOCKADDR*)&saServer, sizeof(SOCKADDR) );
InternetGetLastResponseInfo(&dwError,databuf5,&size);
if( ret == SOCKET_ERROR )
{
InternetGetLastResponseInfo(&dwError,databuf1,&size);
DWORD ERRORRRR;
ERRORRRR =GetLastError();
return FALSE;
}
pFtpCon->Command("REST "+pszOffset,CFtpConnection::CmdRespNone);
InternetGetLastResponseInfo(&dwError,databuf2,&size);
//这里的m_strFtpFilE是指 ftp://file1/file2.exe 中的"file1//file2.exe"
strSeekOffsetCMD="RETR /"+m_strFtpFile;
pFtpCon->Command(strSeekOffsetCMD,CFtpConnection::CmdRespNone);
InternetGetLastResponseInfo(&dwError,databuf3,&size);
//下载文件
int len;
do
{
len = recv(recSocket,buffer,nBufSize,0);
if(len<0)
break;
localFile.Write(buffer,len);
m_nFileTransSize += len;
if(m_bForceStop)
break;
}while( len > 0 );
localFile.Close();