| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181 |
- #include "StdAfx.h"
- #include "Wininet.h"
- #include <shlwapi.h>
- #include <stdio.h>
- #include <tchar.h>
- #include "HttpDownload.h"
- #include <afxinet.h>
- #pragma comment(lib,"Wininet.lib")
- CHttpDownload::CHttpDownload(void)
- {
- }
- CHttpDownload::~CHttpDownload(void)
- {
- }
- UINT ThreadFun(LPVOID lpParam)
- {
- DownloadInfo *info = (DownloadInfo*)lpParam;
- CString httpFile;
- httpFile.Format(_T("%s"),info->httpFile);
- CString storePath ;
- storePath.Format(_T("%s"),info->storePath);
- CString fileName = httpFile.Mid(httpFile.ReverseFind('/')+1);
- Sleep(1000);
- if(DownloadFile2(httpFile,storePath,info))
- {
- ::SendMessage(info->hwnd,WM_USERMSG_UPGRADE_STATE,USER_PARAM_NETWORK_AVAIABLE,0);
- CStdioFile file;
- CString str;
- CStringArray pStrAarryFiles;
- file.Open(_T("d:\\test\\")+fileName,CFile::modeRead,NULL);
- while(file.ReadString(str))
- {
- pStrAarryFiles.Add(str);
- }
- file.Close();
- for(int i=0;i<pStrAarryFiles.GetSize();i++)
- {
- if(i==0)
- ::PostMessage(info->hwnd,WM_USERMSG_UPGRADE_RANGE,pStrAarryFiles.GetSize(),0);
- if(info->stop)
- break;
- if(DownloadFile2(pStrAarryFiles.GetAt(i),storePath,info))
- {
- ::PostMessage(info->hwnd,WM_USERMSG_UPGRADE_STATE,USER_PARAM_FILE_DOWNLOAD_OK,0);
- }
- else
- {
- ::PostMessage(info->hwnd,WM_USERMSG_UPGRADE_STATE,USER_PARAM_FILE_DOWNLOAD_ERROR,0);
- break;
- }
-
- ::PostMessage(info->hwnd,WM_USERMSG_UPGRADE_PROGRESS,i+1,0);
- }
- }
- else
- {
- ::PostMessage(info->hwnd,WM_USERMSG_UPGRADE_STATE,USER_PARAM_NETWORK_NOT_AVAIABLE,0);
- }
- return 0;
- }
- bool DownloadFile2(CString szUrl,CString storePath,DownloadInfo *info)
- {
- char buffer[1024];
- CInternetSession session;
- CStdioFile *httpfile;
-
- try
- {
- httpfile = session.OpenURL(szUrl,1,INTERNET_FLAG_TRANSFER_BINARY | INTERNET_FLAG_RELOAD);
- COleDateTime dlStart = COleDateTime::GetCurrentTime();
- int fileBytes = httpfile->SeekToEnd();
- httpfile->SeekToBegin();
-
- // 在当前目录创建新的目标文件
- CFile localfile(storePath + httpfile->GetFileName(), CFile::modeCreate | CFile::modeWrite | CFile::typeBinary);
- int bytesfetch; // 读取的的字节数
- int byteswrite = 0; // 已经写入的字节数
- double nperc,kbrecv; // 进度条的百分比,获取到的数据大小(Kbs为单位)
- double secs,kbsec; // 记录秒数, 速度(KB/秒)
-
- while (bytesfetch = httpfile->Read(buffer, sizeof(buffer))) // 读取文件
- {
-
- if(info->stop) // 如果取消下载
- {
- localfile.Close(); // 关闭我们的目标文件
- httpfile->Close(); // 关闭远程文件
- delete httpfile; // 删除CStdioFile对象,以防止泄漏
- AfxEndThread(0); // 结束下载线程
- }
- // 根据开始时间与当前时间比较,获取秒数
- COleDateTimeSpan dlElapsed = COleDateTime::GetCurrentTime() - dlStart;
- secs = dlElapsed.GetTotalSeconds();
- byteswrite = byteswrite + bytesfetch; // 设置新的进度条位置
- localfile.Write(buffer, bytesfetch); // 将实际数据写入文件
-
- nperc = (double)byteswrite * 100 / (double)fileBytes; // 进度百分比
- kbrecv = byteswrite / 1024; // 获取收到的数据
- kbsec = kbrecv / secs; // 获取每秒下载多少(KB)
- if(byteswrite%102400==0 && byteswrite/102400>0)
- ::PostMessage(info->hwnd,WM_USERMSG_UPGRADE_CHILD_PROGRESS,nperc,0);
- }
- // 下载完成,关闭文件
- localfile.Close();
- }
- catch(CInternetException *IE)
- {
- CString strerror;
- TCHAR error[255];
- IE->GetErrorMessage(error,255); // 获取错误消息
- strerror = error;
- if(!httpfile)
- delete httpfile;
- IE->Delete(); // 删除异常对象,以防止泄漏
- return false;
- }
- return true;
- }
- bool DownloadFile(CString httpFile,CString storePath)
- {
- CString fileName = httpFile.Mid(httpFile.ReverseFind('/')+1);
- DWORD length=0;
- BYTE buffer[1024];
- memset(buffer,0,1024);
- HINTERNET hInternet;
- hInternet=InternetOpen(_T("Testing"),INTERNET_OPEN_TYPE_PRECONFIG,NULL,NULL,0);
- if (hInternet==NULL)
- {
- return false;
- }
- HINTERNET hUrl;
- hUrl=InternetOpenUrl(hInternet,httpFile,NULL,0,INTERNET_FLAG_RELOAD,0); //
- if (hUrl==NULL)
- {
- InternetCloseHandle(hInternet);
- return false;
- }
- BOOL hwrite;
- DWORD written;
- HANDLE hFile;
-
- hFile=CreateFile(storePath + fileName,GENERIC_WRITE,0,0,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,0);
- if (hFile==INVALID_HANDLE_VALUE)
- {
- InternetCloseHandle(hUrl);
- InternetCloseHandle(hInternet);
- return false;
- }
- BOOL read;
- while(1)
- {
- read=InternetReadFile(hUrl,buffer,sizeof(buffer),&length);
- if(length==0)
- break;
- hwrite=WriteFile(hFile,buffer,sizeof(buffer),&written,NULL);
- if (hwrite==0)
- {
- CloseHandle(hFile);
- InternetCloseHandle(hUrl);
- InternetCloseHandle(hInternet);
- return false;
- }
- }
- CloseHandle(hFile);
- InternetCloseHandle(hUrl);
- InternetCloseHandle(hInternet);
- return true;
- }
- void CHttpDownload::DownloadFileEx(DownloadInfo *info)
- {
-
- CWinThread* pThread = AfxBeginThread(ThreadFun,info);
-
- }
|