码迷,mamicode.com
首页 > 编程语言 > 详细

C++ 根据图片url 批量 下载图片

时间:2018-05-07 22:48:24      阅读:1243      评论:0      收藏:0      [点我收藏+]

标签:资料   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 codeDescription
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 批量 下载图片

标签:资料   sdn   methods   other   byte   bin   adt   frame   using   

原文地址:https://www.cnblogs.com/hellowooorld/p/9005068.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!