使用CFtpconnection类实现文件的断点续传的问题
2006-09-05 11:45
507 查看
下面是我写的下载和上传的线程,其中下载时无法改变服务器端的读文件指针,只是改变了本地文件指
4000
针,所以只有读前面已下部分时不写,这样可以提高一点速度,但还是不算断点续传.而上传就更难以实现了.
下面的程序可以运行,也可以实现ftp的基本功能.
UINT CFtpView::FileThread( LPVOID lpInf )
{
FTP_INFO *lpInfo = (FTP_INFO *) lpInf;
CInternetSession *lpInetSession;
CFtpConnection *lpFtpConnection;
// Allocate a CInternetSession object.
lpInetSession = new CInternetSession(
lpInfo->szAppName, 1,
PRE_CONFIG_INTERNET_ACCESS );
// If the CInternetSession object didn't
// allocate, bail out.
if( lpInetSession == NULL ){
delete lpInfo;
return( 0 );
}
// Attempt to make the Ftp connection.
try{
// We'll use a NULL pointer for the
// user name since GetFtpConnection()
// will use a default value if it
// gets a NULL for it. If a user
// name was given by user, go ahead and
// point to that string.
char *lpUser = NULL;
if( lpInfo->szUser[0] != 0 )
lpUser = lpInfo->szUser;
// The same thing happens here for the
// password. Use a NULL pointer unless
// user gave a password.
char *lpPassword = NULL;
if( lpInfo->szPassword[0] != 0 )
lpPassword = lpInfo->szPassword;
lpFtpConnection =
lpInetSession->GetFtpConnection(
lpInfo->szHost, lpUser, lpPassword );
}
// If GetFtpConnection() throws an exception,
// catch it, clean up, and return.
catch( CInternetException *lpEx ){
lpEx->Delete();
delete lpInetSession;
delete lpInfo;
return( 0 );
}
try {
CInternetFile *pInternetFile;
CFile* pFile = NULL;
CFileFind finder;
BOOL bFile;
DWORD dwLocalFilelen;
DWORD dwFtpFilelen;
//seach localfile and see whether is exist
//and get it's length
// start working for files
BOOL bWorking = finder.FindFile((CString)lpInfo->szLocalFile+"*");
while (bWorking)
{
bWorking = finder.FindNextFile();
bFile = 1;
// skip . and .. files; otherwise, we'd
// recur infinitely!
if (finder.IsDots())
continue;
}
//if the file was exist,get the length
if( bFile ) {
pFile = new CFile(lpInfo->szLocalFile,
CFile::modeRead | CFile::shareDenyNone);
dwLocalFilelen = (DWORD)pFile->GetLength();
}
//if the file wasn't exist,set the length zero;
else {
dwLocalFilelen = 0;
}
if (pFile != NULL) {
pFile->Close();
delete pFile;
}
//seach remote file and see whether is exist
//and get its length
CFtpFileFind *pFtpFind = new CFtpFileFind(lpFtpConnection);
BOOL res = pFtpFind->FindFile( (CString)lpInfo->szFtpFile+"*");
if(res)
{
pFtpFind->FindNextFile();
CString strFilename = pFtpFind->GetFileName();
// if the file was exist,get the length
dwFtpFilelen = (DWORD)pFtpFind->GetLength();
}
//if the file wasn't exist,set the length zero;
else {
dwFtpFilelen = 0;
}
if (pFtpFind != NULL) {
pFtpFind->Close();
delete pFtpFind;
}
CFile wrFile;
DWORD Percent;
DWORD dwStart;
DWORD dwEnd;
float Speed;
DWORD dwTotalReaded = 0;
DWORD dwReaded = 0;
char buffer[1024]; //存放文件数据的buffer
memset(buffer , 0 , 1024);
// Attempt to send the file from
// the local machine to the remote server.
if( lpInfo->bSendFlag ){
//check the remote file see whether it had already sent
if( dwFtpFilelen >= dwLocalFilelen) {
AfxMessageBox( "The file had already sent." );
}
else {
//if it wasnit exist,send it
if(dwFtpFilelen == 0) {
lpFtpConnection->PutFile( lpInfo->szLocalFile,
lpInfo->szFtpFile );
dwFtpFilelen = dwLocalFilelen;
}
//if it existed but wasn't sent completly,continu sent
else {
pInternetFile = lpFtpConnection->OpenFile( lpInfo->szFtpFile,GENERIC_WRITE );
CFile newFile(lpInfo->szLocalFile, CFile::modeRead|CFile::shareDenyNone);
while(dwReaded = newFile.Read(buffer ,1024))//循环读取数据
{
dwTotalReaded += dwReaded;
pInternetFile->Write(buffer ,dwReaded);
Percent = ((float)dwTotalReaded / dwLocalFilelen)*100;
}
newFile.Close( );
}
AfxMessageBox( "The file was sent." );
}
}
// Attempt to retrieve the file from
// the remote server to the local machine.
else {
if( dwFtpFilelen <= dwLocalFilelen) {
AfxMessageBox( "The file had already retrieved." );
}
else {
pInternetFile = lpFtpConnection->OpenFile( lpInfo->szFtpFile );
if( dwLocalFilelen == 0 )
wrFile.Open(lpInfo->szLocalFile, CFile::modeCreate|CFile::modeWrite | CFile::typeBinary|CFile::shareDenyNone);
else {
wrFile.Open(lpInfo->szLocalFile, CFile::modeWrite| CFile::typeBinary|CFile::shareDenyNone);
wrFile.Seek( (LONGLONG)dwLocalFilelen,CFile::begin);
}
while( dwStart = GetTickCount(),dwReaded = pInternetFile->Read(buffer ,1024))//循环读取数据
{
dwTotalReaded += dwReaded;
if( dwTotalReaded <= dwLocalFilelen )
Percent = ((float)dwLocalFilelen / dwFtpFilelen)*100;
else {
wrFile.Write(buffer ,dwReaded);
Percent = ((float)dwTotalReaded / dwFtpFilelen)*100;
}
dwEnd = GetTickCount();
Speed = (float)977/(dwStart - dwEnd);
}
AfxMessageBox( "The file was retrieved." );
wrFile.Close( );
}
}
if(pInternetFile != NULL) {
pInternetFile->Close();
delete pInternetFile;
pInternetFile = NULL;
}
}
catch(CInternetException* e)
{
e->ReportError();
e->Delete();
return 0;
}
// Close the Ftp connection and delete
// the CFtpConnection and CInternetSession
//objects.
lpFtpConnection->Close();
delete lpFtpConnection;
delete lpInetSession;
delete lpInfo;
return( 0 );
}
4000
针,所以只有读前面已下部分时不写,这样可以提高一点速度,但还是不算断点续传.而上传就更难以实现了.
下面的程序可以运行,也可以实现ftp的基本功能.
UINT CFtpView::FileThread( LPVOID lpInf )
{
FTP_INFO *lpInfo = (FTP_INFO *) lpInf;
CInternetSession *lpInetSession;
CFtpConnection *lpFtpConnection;
// Allocate a CInternetSession object.
lpInetSession = new CInternetSession(
lpInfo->szAppName, 1,
PRE_CONFIG_INTERNET_ACCESS );
// If the CInternetSession object didn't
// allocate, bail out.
if( lpInetSession == NULL ){
delete lpInfo;
return( 0 );
}
// Attempt to make the Ftp connection.
try{
// We'll use a NULL pointer for the
// user name since GetFtpConnection()
// will use a default value if it
// gets a NULL for it. If a user
// name was given by user, go ahead and
// point to that string.
char *lpUser = NULL;
if( lpInfo->szUser[0] != 0 )
lpUser = lpInfo->szUser;
// The same thing happens here for the
// password. Use a NULL pointer unless
// user gave a password.
char *lpPassword = NULL;
if( lpInfo->szPassword[0] != 0 )
lpPassword = lpInfo->szPassword;
lpFtpConnection =
lpInetSession->GetFtpConnection(
lpInfo->szHost, lpUser, lpPassword );
}
// If GetFtpConnection() throws an exception,
// catch it, clean up, and return.
catch( CInternetException *lpEx ){
lpEx->Delete();
delete lpInetSession;
delete lpInfo;
return( 0 );
}
try {
CInternetFile *pInternetFile;
CFile* pFile = NULL;
CFileFind finder;
BOOL bFile;
DWORD dwLocalFilelen;
DWORD dwFtpFilelen;
//seach localfile and see whether is exist
//and get it's length
// start working for files
BOOL bWorking = finder.FindFile((CString)lpInfo->szLocalFile+"*");
while (bWorking)
{
bWorking = finder.FindNextFile();
bFile = 1;
// skip . and .. files; otherwise, we'd
// recur infinitely!
if (finder.IsDots())
continue;
}
//if the file was exist,get the length
if( bFile ) {
pFile = new CFile(lpInfo->szLocalFile,
CFile::modeRead | CFile::shareDenyNone);
dwLocalFilelen = (DWORD)pFile->GetLength();
}
//if the file wasn't exist,set the length zero;
else {
dwLocalFilelen = 0;
}
if (pFile != NULL) {
pFile->Close();
delete pFile;
}
//seach remote file and see whether is exist
//and get its length
CFtpFileFind *pFtpFind = new CFtpFileFind(lpFtpConnection);
BOOL res = pFtpFind->FindFile( (CString)lpInfo->szFtpFile+"*");
if(res)
{
pFtpFind->FindNextFile();
CString strFilename = pFtpFind->GetFileName();
// if the file was exist,get the length
dwFtpFilelen = (DWORD)pFtpFind->GetLength();
}
//if the file wasn't exist,set the length zero;
else {
dwFtpFilelen = 0;
}
if (pFtpFind != NULL) {
pFtpFind->Close();
delete pFtpFind;
}
CFile wrFile;
DWORD Percent;
DWORD dwStart;
DWORD dwEnd;
float Speed;
DWORD dwTotalReaded = 0;
DWORD dwReaded = 0;
char buffer[1024]; //存放文件数据的buffer
memset(buffer , 0 , 1024);
// Attempt to send the file from
// the local machine to the remote server.
if( lpInfo->bSendFlag ){
//check the remote file see whether it had already sent
if( dwFtpFilelen >= dwLocalFilelen) {
AfxMessageBox( "The file had already sent." );
}
else {
//if it wasnit exist,send it
if(dwFtpFilelen == 0) {
lpFtpConnection->PutFile( lpInfo->szLocalFile,
lpInfo->szFtpFile );
dwFtpFilelen = dwLocalFilelen;
}
//if it existed but wasn't sent completly,continu sent
else {
pInternetFile = lpFtpConnection->OpenFile( lpInfo->szFtpFile,GENERIC_WRITE );
CFile newFile(lpInfo->szLocalFile, CFile::modeRead|CFile::shareDenyNone);
while(dwReaded = newFile.Read(buffer ,1024))//循环读取数据
{
dwTotalReaded += dwReaded;
pInternetFile->Write(buffer ,dwReaded);
Percent = ((float)dwTotalReaded / dwLocalFilelen)*100;
}
newFile.Close( );
}
AfxMessageBox( "The file was sent." );
}
}
// Attempt to retrieve the file from
// the remote server to the local machine.
else {
if( dwFtpFilelen <= dwLocalFilelen) {
AfxMessageBox( "The file had already retrieved." );
}
else {
pInternetFile = lpFtpConnection->OpenFile( lpInfo->szFtpFile );
if( dwLocalFilelen == 0 )
wrFile.Open(lpInfo->szLocalFile, CFile::modeCreate|CFile::modeWrite | CFile::typeBinary|CFile::shareDenyNone);
else {
wrFile.Open(lpInfo->szLocalFile, CFile::modeWrite| CFile::typeBinary|CFile::shareDenyNone);
wrFile.Seek( (LONGLONG)dwLocalFilelen,CFile::begin);
}
while( dwStart = GetTickCount(),dwReaded = pInternetFile->Read(buffer ,1024))//循环读取数据
{
dwTotalReaded += dwReaded;
if( dwTotalReaded <= dwLocalFilelen )
Percent = ((float)dwLocalFilelen / dwFtpFilelen)*100;
else {
wrFile.Write(buffer ,dwReaded);
Percent = ((float)dwTotalReaded / dwFtpFilelen)*100;
}
dwEnd = GetTickCount();
Speed = (float)977/(dwStart - dwEnd);
}
AfxMessageBox( "The file was retrieved." );
wrFile.Close( );
}
}
if(pInternetFile != NULL) {
pInternetFile->Close();
delete pInternetFile;
pInternetFile = NULL;
}
}
catch(CInternetException* e)
{
e->ReportError();
e->Delete();
return 0;
}
// Close the Ftp connection and delete
// the CFtpConnection and CInternetSession
//objects.
lpFtpConnection->Close();
delete lpFtpConnection;
delete lpInetSession;
delete lpInfo;
return( 0 );
}
相关文章推荐
- iOS开发-使用NSURLSession实现文件断点下载,文件离线续传以及图片上传
- 关于Qt使用QNetworkAccessManager下载文件(实现断点续传功能)
- (四)使用Libcurl下载文件,解决无信号中断,下载中掉电恢复后断点续传问题的源代码
- 使用libcurl实现获取目标文件大小, 下载进度显示, 断点续传等功能
- Windows Azure: 使用Blob的PutBlock实现大文件断点续传
- 【SFTP】使用Jsch实现Sftp文件下载-支持断点续传和进程监控
- (二)使用libcurl实现获取目标文件大小, 下载进度显示, 断点续传等功能
- 文件批量上传的工具,要实现暂停继续、断点续传等功能(使用QtNetwork和QHttpMultiPart,和定时器检查超时)
- 关于Qt使用QNetworkAccessManager下载文件(实现断点续传功能)
- 使用HASH算法实现文件的断点续传
- Android 使用socket通信来实现文件断点续传上传
- php实现的支持断点续传的文件下载类
- 使用nginx搭建集群tomcat8,redis实现session共享,文件共享问题
- C#实现上传文件分割,断点续传上传文件
- XMLHttpRequest实现HTTP协议下文件上传断点续传
- 前端实现文件的断点续传
- Java Web中使用JSPSmartUpload控件实现文件的上传和下载(解决了中文乱码问题)(JSP页面采用GBK编码)
- C#实现上传文件分割,断点续传上传文件
- 使用NSURLConnection实现断点续传。
- 使用NSURLConnection实现大文件断点下载