先大致分成三步做吧
第一步 文件的下載 最基礎(chǔ)
第二步 多線程連接下載文件 提高速度的關(guān)鍵
第三部 斷點(diǎn)續(xù)傳 非常實(shí)用
今天來實(shí)現(xiàn)第一步吧
文件的下載 http協(xié)議的
使用了一個(gè)VC知識庫的Http類 不多說 上代碼
1 //確定按鈕
2 void CHttpDownloadDlg::OnBnClickedOk()
3 {
4 ::CreateThread(NULL,0,DownloadThreadProc,(PVOID)this,NULL,NULL);
5 }
6
7 //退出按鈕
8 void CHttpDownloadDlg::OnBnClickedCancel()
9 {
10 // TODO: 在此添加控件通知處理程序代碼
11 OnCancel();
12 }
13
14 //獲取存儲的文件夾
15 CString CHttpDownloadDlg::GetStorgeFolde(void)
16 {
17 CString strFold;
18 BROWSEINFO bi;
19 TCHAR buffer[MAX_PATH];
20 ZeroMemory(buffer, MAX_PATH);
21 bi.hwndOwner = GetSafeHwnd();
22 bi.pidlRoot = NULL;
23 bi.pszDisplayName = buffer;
24
25 bi.lpszTitle = _T("選擇一個(gè)文件夾");
26 bi.ulFlags = BIF_EDITBOX;
27 bi.lpfn = NULL;
28 bi.lParam = 0;
29 bi.iImage = 0;
30
31 LPITEMIDLIST pList = NULL;
32 if ((pList = SHBrowseForFolder(&bi)) != NULL)
33 {
34 TCHAR path[MAX_PATH];
35 ZeroMemory(path, MAX_PATH);
36 SHGetPathFromIDList(pList, path);
37 strFold = path;
38 UpdateData(FALSE);
39 }
40 return strFold;
41 }
42
43 //線程函數(shù)
44 DWORD WINAPI DownloadThreadProc(LPVOID pDate)
45 {
46 CHttpDownloadDlg * pMainDlg = (CHttpDownloadDlg*)pDate;
47 //pMainDlg->m_DownloadProg.SetPos(30);
48 CHttpSocket HttpSocket;
49 CString strServer , strObject ;
50 USHORT uPort;
51 DWORD dwServerType;
52 long lLenth;
53 const char * pRequestHeader = NULL;
54
55 //通過URL獲取相關(guān)參數(shù)
56 AfxParseURL(pMainDlg->m_strDownloadAddr , dwServerType , strServer , strObject , uPort);
57 pRequestHeader = HttpSocket.FormatRequestHeader((LPTSTR)(LPCTSTR)strServer , (LPTSTR)(LPCTSTR)strObject , lLenth );
58 HttpSocket.Socket();
59 HttpSocket.Connect((LPTSTR)(LPCTSTR)strServer );
60 HttpSocket.SendRequest();
61 HttpSocket.SetTimeout(100);
62
63 char szLength[15];
64 HttpSocket.GetField("Content-Length" , szLength , 15);
65 int iServerState = HttpSocket.GetServerState();
66 int iFileSize = atoi(szLength);
67 pMainDlg->m_DownloadProg.SetRange(0,iFileSize/1024); //設(shè)置進(jìn)度條
68 CFile file;
69 file.Open(pMainDlg->m_strStorgePath , CFile::modeCreate | CFile::modeWrite);
70 char szDate[2048];
71 int iRecvSize = 0; //讀取的文件大小
72 int iCompleteSize = 0;
73 //DWORD dwStartTime , dwEndTime; //暫時(shí)不計(jì)算速度
74 while (iCompleteSize < iFileSize)
75 {
76 //dwStartTime = GetTickCount();
77 iRecvSize = HttpSocket.Receive(szDate , 2048);
78 if (iRecvSize == 0)
79 {
80 ::AfxMessageBox("服務(wù)器關(guān)閉鏈接");
81 break;
82 }
83 if (iRecvSize == -1)
84 {
85 ::AfxMessageBox("接收數(shù)據(jù)超時(shí)");
86 break;
87 }
88 //dwEndTime = GetTickCount();
89 file.Write(szDate , iRecvSize);
90 iCompleteSize += iRecvSize;
91 pMainDlg->m_DownloadProg.SetPos(iCompleteSize / 1024 );
92 }
93 file.Close();
94 pMainDlg->m_DownloadProg.SetPos(0);
95 AfxMessageBox("下載完成");
96 return 1;
97 }
98
99 //獲取存儲路徑 仿造迅雷的獲取存儲名稱 盡量不要用戶使用鍵盤
100 void CHttpDownloadDlg::OnBnClickedGetStorgePath()
101 {
102 UpdateData(TRUE);
103 if(m_strDownloadAddr == "")
104 {
105 AfxMessageBox("請輸入下載地址" , MB_OK);
106 return ;
107 }
108
109 CString strFileName = m_strDownloadAddr;
110 m_strStorgePath = GetStorgeFolde();
111
112 while(strFileName.Find("/") != -1)
113 {
114 int i = strFileName.Find("/");
115 strFileName = strFileName.Mid(i +1 , strFileName.GetLength() - i - 1);
116 }
117
118 m_strStorgePath += strFileName;
119 UpdateData(FALSE);
120 }
121
界面很簡陋 2 void CHttpDownloadDlg::OnBnClickedOk()
3 {
4 ::CreateThread(NULL,0,DownloadThreadProc,(PVOID)this,NULL,NULL);
5 }
6
7 //退出按鈕
8 void CHttpDownloadDlg::OnBnClickedCancel()
9 {
10 // TODO: 在此添加控件通知處理程序代碼
11 OnCancel();
12 }
13
14 //獲取存儲的文件夾
15 CString CHttpDownloadDlg::GetStorgeFolde(void)
16 {
17 CString strFold;
18 BROWSEINFO bi;
19 TCHAR buffer[MAX_PATH];
20 ZeroMemory(buffer, MAX_PATH);
21 bi.hwndOwner = GetSafeHwnd();
22 bi.pidlRoot = NULL;
23 bi.pszDisplayName = buffer;
24
25 bi.lpszTitle = _T("選擇一個(gè)文件夾");
26 bi.ulFlags = BIF_EDITBOX;
27 bi.lpfn = NULL;
28 bi.lParam = 0;
29 bi.iImage = 0;
30
31 LPITEMIDLIST pList = NULL;
32 if ((pList = SHBrowseForFolder(&bi)) != NULL)
33 {
34 TCHAR path[MAX_PATH];
35 ZeroMemory(path, MAX_PATH);
36 SHGetPathFromIDList(pList, path);
37 strFold = path;
38 UpdateData(FALSE);
39 }
40 return strFold;
41 }
42
43 //線程函數(shù)
44 DWORD WINAPI DownloadThreadProc(LPVOID pDate)
45 {
46 CHttpDownloadDlg * pMainDlg = (CHttpDownloadDlg*)pDate;
47 //pMainDlg->m_DownloadProg.SetPos(30);
48 CHttpSocket HttpSocket;
49 CString strServer , strObject ;
50 USHORT uPort;
51 DWORD dwServerType;
52 long lLenth;
53 const char * pRequestHeader = NULL;
54
55 //通過URL獲取相關(guān)參數(shù)
56 AfxParseURL(pMainDlg->m_strDownloadAddr , dwServerType , strServer , strObject , uPort);
57 pRequestHeader = HttpSocket.FormatRequestHeader((LPTSTR)(LPCTSTR)strServer , (LPTSTR)(LPCTSTR)strObject , lLenth );
58 HttpSocket.Socket();
59 HttpSocket.Connect((LPTSTR)(LPCTSTR)strServer );
60 HttpSocket.SendRequest();
61 HttpSocket.SetTimeout(100);
62
63 char szLength[15];
64 HttpSocket.GetField("Content-Length" , szLength , 15);
65 int iServerState = HttpSocket.GetServerState();
66 int iFileSize = atoi(szLength);
67 pMainDlg->m_DownloadProg.SetRange(0,iFileSize/1024); //設(shè)置進(jìn)度條
68 CFile file;
69 file.Open(pMainDlg->m_strStorgePath , CFile::modeCreate | CFile::modeWrite);
70 char szDate[2048];
71 int iRecvSize = 0; //讀取的文件大小
72 int iCompleteSize = 0;
73 //DWORD dwStartTime , dwEndTime; //暫時(shí)不計(jì)算速度
74 while (iCompleteSize < iFileSize)
75 {
76 //dwStartTime = GetTickCount();
77 iRecvSize = HttpSocket.Receive(szDate , 2048);
78 if (iRecvSize == 0)
79 {
80 ::AfxMessageBox("服務(wù)器關(guān)閉鏈接");
81 break;
82 }
83 if (iRecvSize == -1)
84 {
85 ::AfxMessageBox("接收數(shù)據(jù)超時(shí)");
86 break;
87 }
88 //dwEndTime = GetTickCount();
89 file.Write(szDate , iRecvSize);
90 iCompleteSize += iRecvSize;
91 pMainDlg->m_DownloadProg.SetPos(iCompleteSize / 1024 );
92 }
93 file.Close();
94 pMainDlg->m_DownloadProg.SetPos(0);
95 AfxMessageBox("下載完成");
96 return 1;
97 }
98
99 //獲取存儲路徑 仿造迅雷的獲取存儲名稱 盡量不要用戶使用鍵盤
100 void CHttpDownloadDlg::OnBnClickedGetStorgePath()
101 {
102 UpdateData(TRUE);
103 if(m_strDownloadAddr == "")
104 {
105 AfxMessageBox("請輸入下載地址" , MB_OK);
106 return ;
107 }
108
109 CString strFileName = m_strDownloadAddr;
110 m_strStorgePath = GetStorgeFolde();
111
112 while(strFileName.Find("/") != -1)
113 {
114 int i = strFileName.Find("/");
115 strFileName = strFileName.Mid(i +1 , strFileName.GetLength() - i - 1);
116 }
117
118 m_strStorgePath += strFileName;
119 UpdateData(FALSE);
120 }
121

今天就先做到這