📄 httpdownload.cpp
字号:
//===================将要下载的文件名自动转换为中文文件名,只对影音文件更名===============
CString strAutoFilePath=the->m_stLeoDownInfo.strAutoFilePath;
BOOL bRename=the->m_stLeoDownInfo.bRename; //是否用"提示信息"给文件命名
if(!strAutoFilePath.IsEmpty() && the->m_stLeoDownInfo.nBreakRev!=1) //必须是新创建的下载任务
{
CString cs,csTemp,csTemp2;;
int nindex=1;
csTemp="";
csTemp2="";
cs=strAutoFilePath;
csTemp=cs.Right(1);
csTemp2=csTemp;
cs=cs.Left(cs.GetLength()-1);
while(csTemp!="/")
{
csTemp="";
csTemp=cs.Right(1);
csTemp2.Insert(0,(LPCTSTR)csTemp);
cs=cs.Left(cs.GetLength()-1);
}
//add FileName
csTemp2=csTemp2.Right(csTemp2.GetLength()-1);
char szDrive[_MAX_DRIVE];
char szDir[_MAX_DIR];
char szFname[_MAX_FNAME];
char szE[_MAX_EXT];
_splitpath(the->m_stLeoDownInfo.strFilePath,szDrive,szDir,szFname,szE);
csTemp="";
strAutoFilePath="";
csTemp=szDrive;
strAutoFilePath=csTemp;
csTemp=szDir;
strAutoFilePath+=csTemp;
strAutoFilePath+=csTemp2;
the->m_stLeoDownInfo.strAutoFilePath=strAutoFilePath;
}
else
{
the->m_stLeoDownInfo.strAutoFilePath=the->m_stLeoDownInfo.strFilePath;
}
if(bRename && the->m_stLeoDownInfo.nBreakRev!=1)
{
char szDrive[_MAX_DRIVE];
char szDir[_MAX_DIR];
char szFname[_MAX_FNAME];
char szE[_MAX_EXT];
_splitpath(the->m_stLeoDownInfo.strAutoFilePath,szDrive,szDir,szFname,szE);
CString strPath,strE;
strE=szE;
strPath=the->m_stLeoDownInfo.strAutoFilePath;
if(strE==".mp3" || strE==".MP3" || strE==".wma" || strE==".WMA" ||
strE==".ram" || strE==".RAM" || strE==".rm" || strE==".RM" ||
strE==".rmvb" || strE==".RMVB" || strE==".mpg" || strE==".MPG" ||
strE==".mpeg" || strE==".MPEG" || strE==".avi" || strE==".AVI" ||
strE==".wav" || strE==".WAV" || strE==".mid" || strE==".MID" ||
strE==".ra" || strE==".RA")
{
strPath="";
CString str,strPane;
str=szDrive;
strPath=str;
str=szDir;
strPath+=str;
str=the->m_stLeoDownInfo.strComment;
strPane=str.Right(1);
while(strPane==" ")
{
str=str.Left(str.GetLength()-1);
strPane=str.Right(1);
}
strPath+=str; //+file name
str=szE;
strPath+=str;
}
the->m_stLeoDownInfo.strAutoFilePath=strPath;
}
//******重新向注册表和列表框写入一些新的内容,主要是上面程序刚刚获得的数据******
MAIN_REGPARAM pMainRegParam;
pMainRegParam.nPID=the->m_stLeoDownInfo.nPID;
pMainRegParam.nItem=the->m_stLeoDownInfo.nPID-1;
pMainRegParam.nImage=the->m_stLeoDownInfo.nImage;
pMainRegParam.nLength=the->m_stLeoDownInfo.lFileLength;
pMainRegParam.strFilePath=the->m_stLeoDownInfo.strAutoFilePath;
//pMainRegParam.strLocationUrl=the->m_stLeoDownInfo.strLocationUrl;
pMainRegParam.strComment=the->m_stLeoDownInfo.strComment;
pMainRegParam.strRegPath=the->m_stLeoDownInfo.strRegSubPath;
//Write {Image,FileName,FileLength,Comment,URL}
CString sTemp;
if(DownRegistry.Open(nhKeyRoot,pMainRegParam.strRegPath))
{
sTemp="";
sTemp.Format("%ld",pMainRegParam.nLength);
DownRegistry.Write("FileLength",(LPCTSTR)sTemp);
//DownRegistry.Write("URL",pMainRegParam.strLocationUrl);
DownRegistry.Write("FileName",pMainRegParam.strFilePath);
}
DownRegistry.Close();
//******向CMainFrame发出WM_WRITEREGMAIN消息,******
//此消息用于重新写入列表框数据,如果为"1"加入工具提示
::SendMessage(the->m_pMainFrame->m_hWnd,WM_WRITEREGMAIN,1,(LPARAM)&pMainRegParam);
//--------------------第四步--------------------------------------------
//--- 开始下载文件 ---
//----------------------------------------------------------------------
//******创建5个线程,用于多线程下载******
CWinThread *MulpWinThread[5];
LEODOWN_INFO Mulm_stLeoDownInfo[5]; //定义5个LEODOWN_INFO结构变量,用于存储一些必要的数据
for(int i=0;i<5;i++)
{
MulpWinThread[i]=NULL;
}
//取文件长度1/5
long MulLength=the->m_stLeoDownInfo.lFileLength;
long nLength=0;
nLength=MulLength/5;
//如果是新任务,就创建一个空文件
if(the->m_stLeoDownInfo.nBreakRev!=1)
{
CFile pFile;
pFile.Open((LPCTSTR)(the->m_stLeoDownInfo.strAutoFilePath),CFile::modeCreate|CFile::modeWrite|CFile::shareDenyNone);
pFile.SetLength(MulLength); //设置新文件长度
pFile.Close();
}
//******如果是以前没有下载完的任务,就重新开始下载******
if(the->m_stLeoDownInfo.nBreakRev==1)
{
//定义5个断点长度值0~4
long BreakLength0,BreakLength1,BreakLength2,BreakLength3,BreakLength4; //用于开始的长度
long ToLength0,ToLength1,ToLength2,ToLength3,ToLength4; //用于结束的长度
HKEY nhKeyRoot=HKEY_CURRENT_USER;
CLRegistry mDownRegistry;
CString sTemp;
//打开注册表,查询断点下载信息,如果查询出错,说明断点数据已被迫破,下载任务结束
if(mDownRegistry.Open(nhKeyRoot,(LPCTSTR)(the->m_stLeoDownInfo.strRegSubPath)))
{
sTemp=""; //开始读注册表第一个断点信息(开始长度)
if(mDownRegistry.Read("BreakLength0",sTemp))
{
BreakLength0=_ttol((LPCTSTR)sTemp);
}
else
{
//如果出错,将开始长度赋值为0
BreakLength0=0;
}
sTemp=""; //开始读注册表第一个断点信息(结束长度)
if(mDownRegistry.Read("ToLength0",sTemp))
{
ToLength0=_ttol((LPCTSTR)sTemp);
}
else
{
//如果出错,将结束长度赋值为1/5长度减1
ToLength0=nLength-1;
}
sTemp="";
if(mDownRegistry.Read("BreakLength1",sTemp))
{
BreakLength1=_ttol((LPCTSTR)sTemp);
}
else
{
BreakLength1=nLength;
}
sTemp="";
if(mDownRegistry.Read("ToLength1",sTemp))
{
ToLength1=_ttol((LPCTSTR)sTemp);
}
else
{
ToLength1=nLength*2-1;
}
sTemp="";
if(mDownRegistry.Read("BreakLength2",sTemp))
{
BreakLength2=_ttol((LPCTSTR)sTemp);
}
else
{
BreakLength2=nLength*2;
}
sTemp="";
if(mDownRegistry.Read("ToLength2",sTemp))
{
ToLength2=_ttol((LPCTSTR)sTemp);
}
else
{
ToLength2=nLength*3-1;
}
sTemp="";
if(mDownRegistry.Read("BreakLength3",sTemp))
{
BreakLength3=_ttol((LPCTSTR)sTemp);
}
else
{
BreakLength3=nLength*3;
}
sTemp="";
if(mDownRegistry.Read("ToLength3",sTemp))
{
ToLength3=_ttol((LPCTSTR)sTemp);
}
else
{
ToLength3=nLength*4-1;
}
sTemp="";
if(mDownRegistry.Read("BreakLength4",sTemp))
{
BreakLength4=_ttol((LPCTSTR)sTemp);
}
else
{
BreakLength4=nLength*4;
}
sTemp="";
if(mDownRegistry.Read("ToLength4",sTemp))
{
ToLength4=_ttol((LPCTSTR)sTemp);
}
else
{
ToLength4=MulLength;
}
//将取到的数据赋给Mulm_stLeoDownInfo[0]数组
Mulm_stLeoDownInfo[0].lFromeByte=BreakLength0;
Mulm_stLeoDownInfo[0].lToByte=ToLength0;
Mulm_stLeoDownInfo[0].lBreakPointByte=BreakLength0;
Mulm_stLeoDownInfo[1].lFromeByte=BreakLength1;
Mulm_stLeoDownInfo[1].lToByte=ToLength1;
Mulm_stLeoDownInfo[1].lBreakPointByte=BreakLength1;
Mulm_stLeoDownInfo[2].lFromeByte=BreakLength2;
Mulm_stLeoDownInfo[2].lToByte=ToLength2;
Mulm_stLeoDownInfo[2].lBreakPointByte=BreakLength2;
Mulm_stLeoDownInfo[3].lFromeByte=BreakLength3;
Mulm_stLeoDownInfo[3].lToByte=ToLength3;
Mulm_stLeoDownInfo[3].lBreakPointByte=BreakLength3;
Mulm_stLeoDownInfo[4].lFromeByte=BreakLength4;
Mulm_stLeoDownInfo[4].lToByte=ToLength4;
Mulm_stLeoDownInfo[4].lBreakPointByte=BreakLength4;
}
else
{
//打开注册表失败,结束下载任务
the->KillTimerEnd(1);
::MessageBox(NULL,"URL连接错误!","Leo",MB_OK|MB_ICONSTOP);
AfxEndThread(0,TRUE);
return 0;
}
}
else
{
//如果是新任务,开始对Mulm_stLeoDownInfo数组赋初值
Mulm_stLeoDownInfo[0].lFromeByte=0;
Mulm_stLeoDownInfo[0].lToByte=nLength-1;
Mulm_stLeoDownInfo[0].lBreakPointByte=0;
Mulm_stLeoDownInfo[1].lFromeByte=nLength;
Mulm_stLeoDownInfo[1].lToByte=nLength*2-1;
Mulm_stLeoDownInfo[1].lBreakPointByte=nLength;
Mulm_stLeoDownInfo[2].lFromeByte=nLength*2;
Mulm_stLeoDownInfo[2].lToByte=nLength*3-1;
Mulm_stLeoDownInfo[2].lBreakPointByte=nLength*2;
Mulm_stLeoDownInfo[3].lFromeByte=nLength*3;
Mulm_stLeoDownInfo[3].lToByte=nLength*4-1;
Mulm_stLeoDownInfo[3].lBreakPointByte=nLength*3;
Mulm_stLeoDownInfo[4].lFromeByte=nLength*4;
Mulm_stLeoDownInfo[4].lToByte=MulLength;
Mulm_stLeoDownInfo[4].lBreakPointByte=nLength*4;
}
//Initial Mulm_stLeoDownInfo;
for(i=0;i<5;i++)
{
Mulm_stLeoDownInfo[i].fPercent=0.0f;
Mulm_stLeoDownInfo[i].fSpeed=0.0f;
Mulm_stLeoDownInfo[i].lFileLength=MulLength;
Mulm_stLeoDownInfo[i].nSvrState=2;
Mulm_stLeoDownInfo[i].pMainFrame=the->m_pMainFrame;
Mulm_stLeoDownInfo[i].strFilePath=the->m_stLeoDownInfo.strAutoFilePath;
Mulm_stLeoDownInfo[i].strLocationUrl=the->m_stLeoDownInfo.strLocationUrl;
Mulm_stLeoDownInfo[i].nSocketLinkNum=the->m_stLeoDownInfo.nSocketLinkNum;
Mulm_stLeoDownInfo[i].nPID=the->m_stLeoDownInfo.nPID;
Mulm_stLeoDownInfo[i].bIsFileDown=TRUE; //为真,线程正在下载文件
Mulm_stLeoDownInfo[i].strRegSubPath=the->m_stLeoDownInfo.strRegSubPath;
Mulm_stLeoDownInfo[i].nCID=i; //确定是第几个线程
Mulm_stLeoDownInfo[i].nBreakRev=the->m_stLeoDownInfo.nBreakRev;
Mulm_stLeoDownInfo[i].stSocketType.strAcceptType=the->m_stLeoDownInfo.stSocketType.strAcceptType;
Mulm_stLeoDownInfo[i].the=(CWnd*)the;
}
//开始创建5个线程
MulpWinThread[0]=AfxBeginThread(DownLoad,(void*)&Mulm_stLeoDownInfo[0]);
the->m_MulHand[0]=MulpWinThread[0]->m_hThread;
the->m_bIsAfxBegin5Thread0=TRUE;
MulpWinThread[1]=AfxBeginThread(DownLoad,(void*)&Mulm_stLeoDownInfo[1]);
the->m_MulHand[1]=MulpWinThread[1]->m_hThread;
the->m_bIsAfxBegin5Thread1=TRUE;
MulpWinThread[2]=AfxBeginThread(DownLoad,(void*)&Mulm_stLeoDownInfo[2]);
the->m_MulHand[2]=MulpWinThread[2]->m_hThread;
the->m_bIsAfxBegin5Thread2=TRUE;
MulpWinThread[3]=AfxBeginThread(DownLoad,(void*)&Mulm_stLeoDownInfo[3]);
the->m_MulHand[3]=MulpWinThread[3]->m_hThread;
the->m_bIsAfxBegin5Thread3=TRUE;
MulpWinThread[4]=AfxBeginThread(DownLoad,(void*)&Mulm_stLeoDownInfo[4]);
the->m_MulHand[4]=MulpWinThread[4]->m_hThread;
the->m_bIsAfxBegin5Thread4=TRUE;
//******打开第二个计时器,用于计算剩余时间******
the->SetTimerBegin(2);
//------注意1------
//!!!******用于将主线程暂停,判断5个子线程是否下载完毕******!!!
while((Mulm_stLeoDownInfo[0].bIsFileDown || Mulm_stLeoDownInfo[1].bIsFileDown ||
Mulm_stLeoDownInfo[2].bIsFileDown || Mulm_stLeoDownInfo[3].bIsFileDown ||
Mulm_stLeoDownInfo[4].bIsFileDown) && !(the->m_bIsEndThe))
{
Sleep(500);
if(!Mulm_stLeoDownInfo[0].bIsFileDown && !(the->m_stHttpDlInfo.BreakInfo0.bRevData0))
{
Mulm_stLeoDownInfo[0].bIsFileDown=TRUE;
MulpWinThread[0]=AfxBeginThread(DownLoad,(void*)&Mulm_stLeoDownInfo[0]);
the->m_MulHand[0]=MulpWinThread[0]->m_hThread;
the->m_bIsAfxBegin5Thread0=TRUE;
}
if(!Mulm_stLeoDownInfo[1].bIsFileDown && !(the->m_stHttpDlInfo.BreakInfo1.bRevData1))
{
Mulm_stLeoDownInfo[1].bIsFileDown=TRUE;
MulpWinThread[1]=AfxBeginThread(DownLoad,(void*)&Mulm_stLeoDownInfo[1]);
the->m_MulHand[1]=MulpWinThread[1]->m_hThread;
the->m_bIsAfxBegin5Thread1=TRUE;
}
if(!Mulm_stLeoDownInfo[2].bIsFileDown && !(the->m_stHttpDlInfo.BreakInfo2.bRevData2))
{
Mulm_stLeoDownInfo[2].bIsFileDown=TRUE;
MulpWinThread[2]=AfxBeginThread(DownLoad,(void*)&Mulm_stLeoDownInfo[2]);
the->m_MulHand[2]=MulpWinThread[2]->m_hThread;
the->m_bIsAfxBegin5Thread2=TRUE;
}
if(!Mulm_stLeoDownInfo[3].bIsFileDown && !(the->m_stHttpDlInfo.BreakInfo3.bRevData3))
{
Mulm_stLeoDownInfo[3].bIsFileDown=TRUE;
MulpWinThread[3]=AfxBeginThread(DownLoad,(void*)&Mulm_stLeoDownInfo[3]);
the->m_MulHand[3]=MulpWinThread[3]->m_hThread;
the->m_bIsAfxBegin5Thread3=TRUE;
}
if(!Mulm_stLeoDownInfo[4].bIsFileDown && !(the->m_stHttpDlInfo.BreakInfo4.bRevData4))
{
Mulm_stLeoDownInfo[4].bIsFileDown=TRUE;
MulpWinThread[4]=AfxBeginThread(DownLoad,(void*)&Mulm_stLeoDownInfo[4]);
the->m_MulHand[4]=MulpWinThread[4]->m_hThread;
the->m_bIsAfxBegin5Thread4=TRUE;
}
}
//强制退出
if(the->m_bIsEndThe)
{
the->KillTimerEnd(2);
the->KillTimerEnd(1);
AfxEndThread(0,TRUE);
return 0;
}
//下载结束,停止计时器:2,停止计时器:1
the->KillTimerEnd(2);
the->KillTimerEnd(1);
the->m_bIsEndThe=TRUE; //为真,准备结束主线程
//******向注册表和列表框写入一些下载后的信息******
MAIN2_REGPARAM pMain2Param;
pMain2Param.nItem=the->m_stLeoDownInfo.nPID-1;
pMain2Param.strRegPath=the->m_stLeoDownInfo.strRegSubPath;
//向注册表写入图示和百分比
if(DownRegistry.Open(nhKeyRoot,pMain2Param.strRegPath))
{
DownRegistry.Write("Image",0);
DownRegistry.Write("Percent","100%");
}
DownRegistry.Close();
//向CMainFrame发出WM_WRITEREG2消息,此消息用于向列表框写入一些数据
::SendMessage(the->m_pMainFrame->m_hWnd,WM_WRITEREG2,0,(LPARAM)&pMain2Param);
//将CMainFrame中的m_nThreadNumber变量减一,用于去掉工具提示中已经完成的任务提示
::SendMessage(the->m_pMainFrame->m_hWnd,WM_DECRSETHNUM,the->m_stLeoDownInfo.nPID-1,0);
//******查询注册表Leo2005的安装目录,用于找到下载后提示音乐的路径******
CLRegistry dllRegistry;
CString SetupPath;
HKEY dllKey=HKEY_LOCAL_MACHINE;
LPTSTR dllKeyPath="Software\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\Leo2005_is1";
dllRegistry.Open(dllKey,dllKeyPath);
dllRegistry.Read("InstallLocation",SetupPath);
SetupPath+="downok.wav";
CFileFind wavFind;
if(wavFind.FindFile((LPCTSTR)SetupPath))
sndPlaySound((LPCTSTR)SetupPath,SND_ASYNC);
else
::MessageBox(NULL,"文件下载完毕","Leo",MB_OK|MB_ICONINFORMATION);
wavFind.Close();
//结束主线程
AfxEndThread(0,TRUE);
return 0;
}
void CalueName(CString &f1,CString &b2,CString ss)
{
CString cs="",csTemp="",csTemp2="";
int nindex=1;
cs=ss;
csTemp=cs.Right(1);
csTemp2=csTemp;
cs=cs.Left(cs.GetLength()-1);
while(csTemp!="\\")
{
csTemp="";
csTemp=cs.Right(1);
csTemp2.Insert(0,(LPCTSTR)csTemp);
cs=cs.Left(cs.GetLength()-1);
}
//add FileName
b2=csTemp2.Right(csTemp2.GetLength()-1);
f1=ss.Left(ss.GetLength()-(csTemp2.GetLength()+1));
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -