void CFtpFileDlg::ListContent() { CInternetSession* pSession; CFtpConnection* pConnection; CFtpFileFind* pFileFind; CString strFileName; BOOL bContinue; pConnection = NULL; pFileFind = NULL; pSession = new CInternetSession(AfxGetAppName(),1,PRE_CONFIG_INTERNET_ACCESS); try{ pConnection = pSession->GetFtpConnection(m_strFtpSite,m_strName,m_strPwd); } catch(CInternetException *e){ e->Delete(); pConnection = NULL; AfxMessageBox("连接失败!",MB_OK|MB_ICONSTOP); } if (pConnection!= NULL){ pFileFind = new CFtpFileFind(pConnection); bContinue = pFileFind->FindFile(NULL); if (!bContinue){ pFileFind->Close(); pFileFind=NULL; } while(bContinue){ bContinue = pFileFind->FindNextFile(); strFileName = pFileFind->GetFileName(); if (pFileFind->IsDirectory()) { strFileName = "["+strFileName; strFileName += "]"; } m_ListFile.AddString(strFileName); } if (pFileFind!=NULL){ pFileFind->Close(); pFileFind=NULL; } } delete pFileFind; if (pConnection!=NULL){ pConnection->Close(); delete pConnection; } delete pSession; }
void CFtpDownloadThread::Run() { USES_CONVERSION; CInternetSession InternetSession; CFtpConnection * pFtpConn = NULL; CInternetFile * pFtpFile = NULL; CString strFtpUrl, fileDir; CFileException fileException; CFile file; UINT nFtpReadSize = 0; BYTE buffer[1024] = {0}; BOOL bIsOpenFile = FALSE; CFtpFileFind * pFtpFind = NULL; strFtpUrl.Format(_T("%s"), A2W(m_strFptURL.c_str())); try { pFtpConn = InternetSession.GetFtpConnection(strFtpUrl, m_strUsr.c_str()?NULL:m_strUsr.c_str(), m_strPwd.c_str()?NULL:m_strPwd.c_str(), m_nftpPort); } catch (CInternetException * pEx) { TCHAR sz[1024]; pEx->GetErrorMessage(sz, 1024); AfxMessageBox(sz); pEx->Delete(); m_nErrorCode = FTP_SERVER_CONNECT_FAILED; goto end; } if (pFtpConn == NULL) { m_nErrorCode = FTP_SERVER_CONNECT_FAILED; goto end; } fileDir.Format(_T("%s\\%s"), m_strSaveDir.c_str(), m_strFileName.c_str()); UINT nMode = 0; //CInternetFile不支持FTP协议的端点续传,需要自己实现。 /*if (PathFileExists(fileDir)) nMode = CFile::modeWrite; else*/ nMode = CFile::modeCreate | CFile::modeReadWrite | CFile::typeBinary; if (!file.Open(fileDir, nMode, &fileException)){ m_nErrorCode = ERR_OPEN_LOACAL_SAVE_FILE; goto end; } bIsOpenFile = TRUE; file.SeekToEnd(); m_nDownloadLen = file.GetLength(); pFtpFind = new CFtpFileFind(pFtpConn); if (!pFtpFind->FindFile(m_strFileName.c_str())) { m_nErrorCode = FTP_SERVER_FILE_OPEN_ERR; pFtpFind->Close(); delete pFtpFind; goto end; } pFtpFind->FindNextFile(); m_nFtpFileLen = pFtpFind->GetLength(); pFtpFind->Close(); delete pFtpFind; pFtpFile = pFtpConn->OpenFile(m_strFileName.c_str()); if (pFtpFile == NULL) { m_nErrorCode = FTP_SERVER_FILE_OPEN_ERR; goto end; } /*//CInternetFile不支持FTP协议的断点续传,需要自己实现。 if (m_nFtpFileLen != 0) pFtpFile->Seek(m_nDownloadLen, CInternetFile::begin);*/ while(TRUE) { if (!m_bIsStart) break; if (m_nDownloadLen >= m_nFtpFileLen) break; try { nFtpReadSize = pFtpFile->Read(buffer, sizeof(buffer)); } catch (CInternetException * pEx) { TCHAR sz[1024]; pEx->GetErrorMessage(sz, 1024); AfxMessageBox(sz); pEx->Delete(); m_nErrorCode = FTP_SERVER_CONNECT_FAILED; goto end; } if (nFtpReadSize <= 0) continue; try { file.Write(buffer, sizeof(buffer)); }catch(CFileException * pEx) { TCHAR sz[1024]; pEx->GetErrorMessage(sz, 1024); AfxMessageBox(sz); pEx->Delete(); m_nErrorCode = ERR_WRITE_LOCAL_FILE; goto end; } m_nDownloadLen += nFtpReadSize; } end: if (pFtpFile) { pFtpFile->Close(); delete pFtpFile; } if (pFtpConn) { pFtpConn->Close(); delete pFtpConn; } InternetSession.Close(); if (bIsOpenFile) file.Close(); m_bIsStart = FALSE; }