花了一天的時間整出來這點東西,寫一下吧!
[cpp]
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;
//讀寫文件 www.2cto.com
while(len=localFile.Read(buffer,nBufSize))
{
pInetFile->Write(buffer,len);
m_nFileTransSize += len;
}
localFile.Close();
return true;
}