18,356
社区成员
发帖
与我相关
我的任务
分享
DWORD WINAPI CUpdateDlg::DownloadFileFromWeb(LPVOID lParam)
{
CUpdateDlg *pDlg=(CUpdateDlg *)lParam;
pDlg->strFileURLInServer != "";
pDlg->strFileLocalFullPath != "";
CInternetSession session;
CHttpConnection* pHttpConnection = NULL;
CHttpFile* pHttpFile = NULL;
CString strServer, strObject;
INTERNET_PORT wPort;
BOOL bReturn = FALSE;
DWORD dwType;
const int nTimeOut = 2000;
session.SetOption(INTERNET_OPTION_CONNECT_TIMEOUT, nTimeOut); //重试之间的等待延时
session.SetOption(INTERNET_OPTION_CONNECT_RETRIES, 1); //重试次数
char* pszBuffer = NULL;
try
{
AfxParseURL(pDlg->strFileURLInServer, dwType, strServer, strObject, wPort);
pHttpConnection = session.GetHttpConnection(strServer, wPort);
pHttpFile = pHttpConnection->OpenRequest(CHttpConnection::HTTP_VERB_GET, strObject);
if(pHttpFile->SendRequest() == FALSE)
return FALSE;
DWORD dwStateCode;
pHttpFile->QueryInfoStatusCode(dwStateCode);
if(dwStateCode == HTTP_STATUS_OK)
{
HANDLE hFile = CreateFile(pDlg->strFileLocalFullPath, GENERIC_WRITE,
FILE_SHARE_WRITE, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL,
NULL); //创建本地文件
if(hFile == INVALID_HANDLE_VALUE)
{
pHttpFile->Close();
pHttpConnection->Close();
session.Close();
return FALSE;
}
char szInfoBuffer[1000]; //返回消息
DWORD dwFileSize = 0; //文件长度
DWORD dwInfoBufferSize = sizeof(szInfoBuffer);
BOOL bResult = FALSE;
bResult = pHttpFile->QueryInfo(HTTP_QUERY_CONTENT_LENGTH,
(void*)szInfoBuffer,&dwInfoBufferSize,NULL);
dwFileSize = atoi(szInfoBuffer);
const int BUFFER_LENGTH = 1024 * 10;
pszBuffer = new char[BUFFER_LENGTH]; //读取文件的缓冲
DWORD dwWrite, dwTotalWrite;
dwWrite = dwTotalWrite = 0;
UINT nRead = pHttpFile->Read(pszBuffer, BUFFER_LENGTH); //读取服务器上数据
ofstream fileOut("d:\\TestOut.txt", ios::app);
CString sDate;
time_t curUtcTime;
struct tm *curTime;
while(nRead > 0)
{
time(&curUtcTime);
curTime = localtime(&curUtcTime);
sDate.Format("%02d:%02d:%02d", curTime->tm_hour, curTime->tm_min, curTime->tm_sec);
fileOut << sDate << "start write" << endl;
WriteFile(hFile, pszBuffer, nRead, &dwWrite, NULL); //写到本地文件
ZeroMemory(pszBuffer, BUFFER_LENGTH);
dwTotalWrite += dwWrite;
time(&curUtcTime);
curTime = localtime(&curUtcTime);
sDate.Format("%02d:%02d:%02d", curTime->tm_hour, curTime->tm_min, curTime->tm_sec);
fileOut << sDate << "start read" << endl;
nRead = pHttpFile->Read(pszBuffer, BUFFER_LENGTH);
pDlg->nPecent += 1000 * nRead / ((float)dwFileSize);
//m_progressDownload.SetPos((int)nPecent);
//Sleep(5);
}
pDlg->nPecent = 1000;
//GetDlgItem(CUpdateDlg.m_hWnd, uid)->SetPos((int)nPecent);
//m_progressDownload.SetPos((int)nPecent);
delete[]pszBuffer;
pszBuffer = NULL;
CloseHandle(hFile);
bReturn = TRUE;
}
}
catch(CInternetException* e)
{
TCHAR tszErrString[256];
e->GetErrorMessage(tszErrString, sizeof(tszErrString));
TRACE(_T("Download XSL error! URL: %s,Error: %s"), pDlg->strFileURLInServer, tszErrString);
e->Delete();
}
catch(...)
{
}
if(pszBuffer != NULL)
{
delete[]pszBuffer;
}
if(pHttpFile != NULL)
{
pHttpFile->Close();
delete pHttpFile;
}
if(pHttpConnection != NULL)
{
pHttpConnection->Close();
delete pHttpConnection;
}
session.Close();
return bReturn;
}