FTP斷點續傳

來源:互聯網
上載者:User

筆記:

重要筆記:

簡單的FTP檔案讀取

/傳送Ftp檔案,失敗返回false,成功返回true
下面是個簡單的FTP上傳例子(不續傳)
bool CFtpTransThread::FtpTransProc(void){    //開始傳送    CInternetSession session;    CFtpConnection *pFtpCon = NULL;    CInternetFile *pInetFile = NULL;    CFile localFile;      localFile.Open(m_strSourceFile,CFile::modeRead|CFile::shareDenyNone);         pFtpCon->CreateDirectory(m_ftpPath);                while( len=localFile.Read(buffer,nBufSize))        {            pInetFile->Write(buffer,len);           m_nFileTransSize += len;                if(m_bForceStop)                break;        }}
下面是個簡單的上傳續傳的例子
bool CFtpTransThread::FtpTransProc(void){//開始傳送CInternetSession session;CFtpConnection *pFtpCon = NULL;CInternetFile *pInetFile = NULL;CFile localFile;         localFile.Open(m_strSourceFile,CFile::modeRead|CFile::shareDenyNone);         pFtpCon->CreateDirectory(m_ftpPath);
//是指路徑:如FTP://file1/file2.rar 則是"file1//file2.rar"
         pInetFile=pFtpCon->Command("APPE " +m_ftpPath,CFtpConnection::CmdRespWrite);
while( len=localFile.Read(buffer,nBufSize))
{pInetFile->Write(buffer,len);m_cs.Lock();m_nFileTransSize += len;m_cs.Unlock();if(m_bForceStop)break;}}
簡單FTP下載(不續傳)
代碼pInetFile = pFtpCon->OpenFile(m_strFtpFile,GENERIC_READ);           
 int len;           
 while( len=pInetFile->Read(buffer,nBufSize))           
 {              
  localFile.Write(buffer,len);               
         m_nFileTransSize += len;             
     if(m_bForceStop)                  
  break;         
  }

 

簡單FTP下載(續傳)有點麻煩
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;CStringstrSeekOffsetCMD = "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 socketrecSocket = socket(AF_INET, SOCK_STREAM, 0);// Get the local host informationremoteHost = gethostbyname(m_ftpIp);remoteIP = inet_ntoa (*(struct in_addr *)*remoteHost->h_addr_list);// Set up the sockaddr structuresaServer.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 structureInternetGetLastResponseInfo(&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();

 

擷取FTP上檔案大小
GetFtpFileSize(CFtpConnection* pFtpCon, CString strFtpFile){CFtpFileFind   ftpFind(pFtpCon); LONGLONG filelen = 0;if(ftpFind.FindFile(strFtpFile)) { ftpFind.FindNextFile(); filelen =   ftpFind.GetLength(); } ftpFind.Close(); return filelen;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.