HttpDownload.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. #include "StdAfx.h"
  2. #include "Wininet.h"
  3. #include <shlwapi.h>
  4. #include <stdio.h>
  5. #include <tchar.h>
  6. #include "HttpDownload.h"
  7. #include <afxinet.h>
  8. #pragma comment(lib,"Wininet.lib")
  9. CHttpDownload::CHttpDownload(void)
  10. {
  11. }
  12. CHttpDownload::~CHttpDownload(void)
  13. {
  14. }
  15. UINT ThreadFun(LPVOID lpParam)
  16. {
  17. DownloadInfo *info = (DownloadInfo*)lpParam;
  18. CString httpFile;
  19. httpFile.Format(_T("%s"),info->httpFile);
  20. CString storePath ;
  21. storePath.Format(_T("%s"),info->storePath);
  22. CString fileName = httpFile.Mid(httpFile.ReverseFind('/')+1);
  23. Sleep(1000);
  24. if(DownloadFile2(httpFile,storePath,info))
  25. {
  26. ::SendMessage(info->hwnd,WM_USERMSG_UPGRADE_STATE,USER_PARAM_NETWORK_AVAIABLE,0);
  27. CStdioFile file;
  28. CString str;
  29. CStringArray pStrAarryFiles;
  30. file.Open(_T("d:\\test\\")+fileName,CFile::modeRead,NULL);
  31. while(file.ReadString(str))
  32. {
  33. pStrAarryFiles.Add(str);
  34. }
  35. file.Close();
  36. for(int i=0;i<pStrAarryFiles.GetSize();i++)
  37. {
  38. if(i==0)
  39. ::PostMessage(info->hwnd,WM_USERMSG_UPGRADE_RANGE,pStrAarryFiles.GetSize(),0);
  40. if(info->stop)
  41. break;
  42. if(DownloadFile2(pStrAarryFiles.GetAt(i),storePath,info))
  43. {
  44. ::PostMessage(info->hwnd,WM_USERMSG_UPGRADE_STATE,USER_PARAM_FILE_DOWNLOAD_OK,0);
  45. }
  46. else
  47. {
  48. ::PostMessage(info->hwnd,WM_USERMSG_UPGRADE_STATE,USER_PARAM_FILE_DOWNLOAD_ERROR,0);
  49. break;
  50. }
  51. ::PostMessage(info->hwnd,WM_USERMSG_UPGRADE_PROGRESS,i+1,0);
  52. }
  53. }
  54. else
  55. {
  56. ::PostMessage(info->hwnd,WM_USERMSG_UPGRADE_STATE,USER_PARAM_NETWORK_NOT_AVAIABLE,0);
  57. }
  58. return 0;
  59. }
  60. bool DownloadFile2(CString szUrl,CString storePath,DownloadInfo *info)
  61. {
  62. char buffer[1024];
  63. CInternetSession session;
  64. CStdioFile *httpfile;
  65. try
  66. {
  67. httpfile = session.OpenURL(szUrl,1,INTERNET_FLAG_TRANSFER_BINARY | INTERNET_FLAG_RELOAD);
  68. COleDateTime dlStart = COleDateTime::GetCurrentTime();
  69. int fileBytes = httpfile->SeekToEnd();
  70. httpfile->SeekToBegin();
  71. // 在当前目录创建新的目标文件
  72. CFile localfile(storePath + httpfile->GetFileName(), CFile::modeCreate | CFile::modeWrite | CFile::typeBinary);
  73. int bytesfetch; // 读取的的字节数
  74. int byteswrite = 0; // 已经写入的字节数
  75. double nperc,kbrecv; // 进度条的百分比,获取到的数据大小(Kbs为单位)
  76. double secs,kbsec; // 记录秒数, 速度(KB/秒)
  77. while (bytesfetch = httpfile->Read(buffer, sizeof(buffer))) // 读取文件
  78. {
  79. if(info->stop) // 如果取消下载
  80. {
  81. localfile.Close(); // 关闭我们的目标文件
  82. httpfile->Close(); // 关闭远程文件
  83. delete httpfile; // 删除CStdioFile对象,以防止泄漏
  84. AfxEndThread(0); // 结束下载线程
  85. }
  86. // 根据开始时间与当前时间比较,获取秒数
  87. COleDateTimeSpan dlElapsed = COleDateTime::GetCurrentTime() - dlStart;
  88. secs = dlElapsed.GetTotalSeconds();
  89. byteswrite = byteswrite + bytesfetch; // 设置新的进度条位置
  90. localfile.Write(buffer, bytesfetch); // 将实际数据写入文件
  91. nperc = (double)byteswrite * 100 / (double)fileBytes; // 进度百分比
  92. kbrecv = byteswrite / 1024; // 获取收到的数据
  93. kbsec = kbrecv / secs; // 获取每秒下载多少(KB)
  94. if(byteswrite%102400==0 && byteswrite/102400>0)
  95. ::PostMessage(info->hwnd,WM_USERMSG_UPGRADE_CHILD_PROGRESS,nperc,0);
  96. }
  97. // 下载完成,关闭文件
  98. localfile.Close();
  99. }
  100. catch(CInternetException *IE)
  101. {
  102. CString strerror;
  103. TCHAR error[255];
  104. IE->GetErrorMessage(error,255); // 获取错误消息
  105. strerror = error;
  106. if(!httpfile)
  107. delete httpfile;
  108. IE->Delete(); // 删除异常对象,以防止泄漏
  109. return false;
  110. }
  111. return true;
  112. }
  113. bool DownloadFile(CString httpFile,CString storePath)
  114. {
  115. CString fileName = httpFile.Mid(httpFile.ReverseFind('/')+1);
  116. DWORD length=0;
  117. BYTE buffer[1024];
  118. memset(buffer,0,1024);
  119. HINTERNET hInternet;
  120. hInternet=InternetOpen(_T("Testing"),INTERNET_OPEN_TYPE_PRECONFIG,NULL,NULL,0);
  121. if (hInternet==NULL)
  122. {
  123. return false;
  124. }
  125. HINTERNET hUrl;
  126. hUrl=InternetOpenUrl(hInternet,httpFile,NULL,0,INTERNET_FLAG_RELOAD,0); //
  127. if (hUrl==NULL)
  128. {
  129. InternetCloseHandle(hInternet);
  130. return false;
  131. }
  132. BOOL hwrite;
  133. DWORD written;
  134. HANDLE hFile;
  135. hFile=CreateFile(storePath + fileName,GENERIC_WRITE,0,0,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,0);
  136. if (hFile==INVALID_HANDLE_VALUE)
  137. {
  138. InternetCloseHandle(hUrl);
  139. InternetCloseHandle(hInternet);
  140. return false;
  141. }
  142. BOOL read;
  143. while(1)
  144. {
  145. read=InternetReadFile(hUrl,buffer,sizeof(buffer),&length);
  146. if(length==0)
  147. break;
  148. hwrite=WriteFile(hFile,buffer,sizeof(buffer),&written,NULL);
  149. if (hwrite==0)
  150. {
  151. CloseHandle(hFile);
  152. InternetCloseHandle(hUrl);
  153. InternetCloseHandle(hInternet);
  154. return false;
  155. }
  156. }
  157. CloseHandle(hFile);
  158. InternetCloseHandle(hUrl);
  159. InternetCloseHandle(hInternet);
  160. return true;
  161. }
  162. void CHttpDownload::DownloadFileEx(DownloadInfo *info)
  163. {
  164. CWinThread* pThread = AfxBeginThread(ThreadFun,info);
  165. }