FTP断点续传

花了一天的时间整出来这点东西,写一下吧!

VOID FtpThief::Connect(TCHAR*IP,TCHAR*USER,TCHAR*PASS,UINT PORT) 
{
	pInternetSession = new CInternetSession("MR",INTERNET_OPEN_TYPE_PRECONFIG);
	try
	{
		pFtpConnection = pInternetSession->GetFtpConnection(IP,USER,PASS,PORT);
		pFtpConnection->CreateDirectory("web\\uploadfile");  

		bconnect=TRUE;
	}catch(CInternetException* pEx)
	{
		TCHAR szErr[1024];
		pEx->GetErrorMessage(szErr, 1024);
		printf("错误:%s\n",szErr);
		pEx->Delete();
	}

}

//获取FTP上文件大小
 LONGLONG  FtpThief::GetFtpFileSize(CFtpConnection* pFtpCon, CString strFtpFile)
{
	 CFtpFileFind   ftpFind(pFtpCon); 
	 LONGLONG filelen = 0;
	 if(ftpFind.FindFile(strFtpFile)) 
	 { 
	 ftpFind.FindNextFile(); 
	 filelen =   ftpFind.GetLength(); 
	 } 
	 ftpFind.Close(); 
	 return filelen;
} 

 //断点续传
bool FtpThief::FtpTransProc(TCHAR*FilePath,TCHAR*FileName)
{
	CString m_ftpPath = FileName;
	CFile localFile;
	DWORD nRet = localFile.Open(FilePath,CFile::modeRead|CFile::shareDenyRead);

	if(!nRet)
	{
		OutputDebugString("open file error");
		return false;
	}
	//获取文件大小,设置续传文件的位置
	long long llFileBegin;
	llFileBegin = GetFtpFileSize(pFtpConnection,m_ftpPath);
	localFile.Seek(llFileBegin,CFile::begin);

	///pFtpConnection->CreateDirectory(m_ftpPath);  
	//是指路径:如FTP://file1/file2.rar 则是"file1//file2.rar"
    
	CInternetFile *pInetFile = NULL;
	pInetFile=pFtpConnection->Command("APPE " +m_ftpPath,CFtpConnection::CmdRespWrite);
	DWORD len;
	long long m_nFileTransSize = 0;
	char buffer[MAX_PATH*1024*2] = {0};
	DWORD nBufSize = MAX_PATH*1024*2;
	//读写文件
	while(len=localFile.Read(buffer,nBufSize))
	{
		pInetFile->Write(buffer,len);
		m_nFileTransSize += len;
	}
	localFile.Close();
	return true;
}


 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值