FTP断点续传

来源:互联网 发布:js dom 添加属性 编辑:程序博客网 时间:2024/04/27 02:11
FTP断点续传
Posted on 2010-08-24 15:55 zhaohongyu113 阅读(560) 评论(0) 编辑 收藏  
笔记:
重要笔记:
简单的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;
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();






 


获取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;
}
原创粉丝点击