#include "StdAfx.h" #include "Wininet.h" #include #include #include #include "HttpDownload.h" #include #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;ihwnd,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); }