標籤:資料 sdn methods other byte bin adt frame using
最近需要用到根據圖片URL批量下載到本地的操作。尋找了相關資料,記錄在這兒。
1.首先在CSV檔案中提取出url
ifstream fin("C:\\Users\\lenovo\\Desktop\\query_result0503.csv"); //開啟檔案流操作 string line; int cnt = 0; while (getline(fin, line) && cnt < 20) { istringstream sin(line); //將整行字串line讀入到字串流istringstream中 vector<string> urls; string url; while (getline(sin, url, ‘,‘)) //以逗號為分隔字元 { urls.push_back(url); } cnt++; size_t found = urls[0].find_last_of("/\\"); if (found == string::npos) continue; string imgname = urls[0].substr(found+1); cout << "處理後:" << urls[0] << "----" <<cnt<<"----" <<imgname << endl;
}
2.根據URL將圖片儲存到本地。
需要用到 URLDownloadToFile函數 。
踩的坑主要是 這個函數需要用到寬字元參數。。如果參數類型不正確。。那麼你甭想下載得到正確檔案。。。
需要用到 MultiByteToWideChar函數來轉換一下。。。。
HRESULT URLDownloadToFile( LPUNKNOWN pCaller, LPCTSTR szURL, LPCTSTR szFileName, _Reserved_ DWORD dwReserved, LPBINDSTATUSCALLBACK lpfnCB);
Parameters
pCaller
A pointer to the controlling IUnknown interface of the calling ActiveX component, if the caller is an ActiveX component. If the calling application is not an ActiveX component, this value can be set to NULL. Otherwise, the caller is a COM object that is contained in another component, such as an ActiveX control in the context of an HTML page. This parameter represents the outermost IUnknown of the calling component. The function attempts the download in the context of the ActiveX client framework, and allows the caller container to receive callbacks on the progress of the download.
szURL
A pointer to a string value that contains the URL to download. Cannot be set to NULL. If the URL is invalid, INET_E_DOWNLOAD_FAILURE is returned.
szFileName
A pointer to a string value containing the name or full path of the file to create for the download. If szFileNameincludes a path, the target directory must already exist.
dwReserved
Reserved. Must be set to 0.
lpfnCB
A pointer to the IBindStatusCallback interface of the caller. By using IBindStatusCallback::OnProgress, a caller can receive download status. URLDownloadToFile calls the IBindStatusCallback::OnProgress and IBindStatusCallback::OnDataAvailable methods as data is received. The download operation can be canceled by returning E_ABORT from any callback. This parameter can be set to NULL if status is not required.
Return value
This function can return one of these values.
Return code |
Description |
S_OK |
The download started successfully. |
E_OUTOFMEMORY |
The buffer length is invalid, or there is insufficient memory to complete the operation. |
INET_E_DOWNLOAD_FAILURE |
The specified resource or callback interface was invalid. |
/// size_t len = urls[0].length();//擷取字串長度 int nmlen = MultiByteToWideChar(CP_ACP, 0, urls[0].c_str(), len + 1, NULL, 0);//如果函數運行成功,並且cchWideChar為零, //傳回值是接收到待轉換字串的緩衝區所需求的寬字元數大小。 wchar_t* buffer = new wchar_t[nmlen]; MultiByteToWideChar(CP_ACP, 0, urls[0].c_str(), len + 1, buffer, nmlen); string savepath = "C:\\Users\\lenovo\\Desktop\\csvfile\\query_result0423\\"+imgname; size_t len1 = savepath.length(); wchar_t* imgsavepath = new wchar_t[len1]; int nmlen1 = MultiByteToWideChar(CP_ACP, 0, savepath.c_str(), len1 + 1, NULL, 0); MultiByteToWideChar(CP_ACP, 0, savepath.c_str(), len1 + 1, imgsavepath, nmlen1); HRESULT hr = URLDownloadToFile(NULL, buffer, imgsavepath, 0, NULL); if (hr == S_OK) { cout << "-------ok" << endl; }
參考:
http://www.cnblogs.com/codingmengmeng/p/6258020.html
https://docs.microsoft.com/en-us/previous-versions/windows/internet-explorer/ie-developer/platform-apis/ms775123(v=vs.85)#parameters
C++ 根據圖片url 批量 下載圖片