求好点的FTP断点续传的类或是代码

cky77 2019-04-29 04:26:52
RT,下面是我找到的两段代码,都不能用。断点续传是不是必须要用FTP的原始命令呢?
CInternetSession session;
CFtpConnection *pFtp=session.GetFtpConnection(FTP服务器地址,用户名,密码);
CInternetFile *pFtpFile=pFtp->OpenFile(文件路径);CFile file;
int DownLength=0;//已经下载了的数据长度
if(目的目录下没有指定文件)//从头开始下载
{
file.Open(目的文件路径,CFile::modeCreate|CFile::modeWrite);
}
else//断点续传
{
file.Open(目的文件路径,CFile::modeWrite);
file.SeekToEnd();
DownLength=file.GetLength();
pFtpFile->Seek(DownLength,CFile::begin);
}
int SrcLength=pFtpFile->GetLength();
while(DownLength<SrcLength)
{
读源文件;
写目的文件;
}


if(!localFile.Open(m_strLocalFolder + vedioFileName ,CFile::modeWrite|CFile::modeNoTruncate|CFile::modeCreate|CFile::shareDenyNone))
return false;

//设置文件大小

CFileStatus fileStatus;
localFile.GetStatus(fileStatus);
m_nFileTransSize=fileStatus.m_size;
localFile.SeekToEnd();
CString pszOffset;
pszOffset.Format("%d",m_nFileTransSize);
HINTERNET hResponse;
CString strSeekOffsetCMD = "RETR ";

DWORD dwError;
TCHAR databuf1[200];
TCHAR databuf2[200];
TCHAR databuf3[200];
TCHAR databuf4[200];
TCHAR databuf5[200];
DWORD size=200;




pFtpCon->Command("TYPE I",CFtpConnection::CmdRespNone);
InternetGetLastResponseInfo(&dwError,databuf1,&size);
//这里的m_strFtpFilE是指 ftp://file1/file2.exe 中的"file1//file2.exe"
pFtpCon->Command("SIZE /"+m_strFtpFile,CFtpConnection::CmdRespNone);
InternetGetLastResponseInfo(&dwError,databuf1,&size);

pFtpCon->Command("PASV",CFtpConnection::CmdRespNone);
InternetGetLastResponseInfo(&dwError,databuf1,&size);
int dataPort=ParsePasv(databuf1);

pFtpCon->Command("PASV",CFtpConnection::CmdRespNone);
InternetGetLastResponseInfo(&dwError,databuf1,&size);
dataPort=ParsePasv(databuf1);

SOCKET recSocket;
sockaddr_in saServer;
hostent* remoteHost;
char* remoteIP;

// Create a listening socket
recSocket = socket(AF_INET, SOCK_STREAM, 0);

// Get the local host information
remoteHost = gethostbyname(m_ftpIp);
remoteIP = inet_ntoa (*(struct in_addr *)*remoteHost->h_addr_list);

// Set up the sockaddr structure
saServer.sin_family = AF_INET;
saServer.sin_addr.s_addr = inet_addr(remoteIP);
saServer.sin_port = htons(dataPort);

// Bind the listening socket using the
// information in the sockaddr structure
InternetGetLastResponseInfo(&dwError,databuf4,&size);
int ret = connect( recSocket, (SOCKADDR*)&saServer, sizeof(SOCKADDR) );
InternetGetLastResponseInfo(&dwError,databuf5,&size);
if( ret == SOCKET_ERROR )
{
InternetGetLastResponseInfo(&dwError,databuf1,&size);
DWORD ERRORRRR;
ERRORRRR =GetLastError();
return FALSE;
}


pFtpCon->Command("REST "+pszOffset,CFtpConnection::CmdRespNone);
InternetGetLastResponseInfo(&dwError,databuf2,&size);
//这里的m_strFtpFilE是指 ftp://file1/file2.exe 中的"file1//file2.exe"

strSeekOffsetCMD="RETR /"+m_strFtpFile;

pFtpCon->Command(strSeekOffsetCMD,CFtpConnection::CmdRespNone);

InternetGetLastResponseInfo(&dwError,databuf3,&size);

//下载文件

int len;

do

{

len = recv(recSocket,buffer,nBufSize,0);

if(len<0)

break;

localFile.Write(buffer,len);

m_nFileTransSize += len;

if(m_bForceStop)

break;

}while( len > 0 );

localFile.Close();
...全文
269 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
hurryboylqs 2019-04-30
  • 打赏
  • 举报
回复
curl就可以
cky77 2019-04-30
  • 打赏
  • 举报
回复
引用 1 楼 zgl7903 的回复:
服务器是否支持? 其它现有的软件能成功吗? 参考下 curlftpfs 的实现方式

服务器支持断点续传。我想找到的客户端断点上传下载代码哥
zgl7903 2019-04-29
  • 打赏
  • 举报
回复
服务器是否支持? 其它现有的软件能成功吗? 参考下 curlftpfs 的实现方式

18,356

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 网络编程
c++c语言开发语言 技术论坛(原bbs)
社区管理员
  • 网络编程
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧