花了一天的时间整出来这点东西,写一下吧!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
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; } |
转载请注明:exchen's blog » FTP断点续传