C++简单的下载文件操作的封装

  • 头文件

#include "targetver.h"

#include <stdio.h>
#include <tchar.h>

//
#include <Windows.h>
#include <iostream>
#include <vector>
#include <string>

// TODO: 在此处引用程序需要的其他头文件
#include <urlmon.h>
#pragma comment(lib,"urlmon.lib")
class HttpDownloader{
public:
	HttpDownloader();
	~HttpDownloader();
	bool DownloadRes(std::wstring url,std::wstring res_name=L""); //下载资源
};

  • 实现文件
#include "HttpDownloader.h"
using namespace std;
//实现部分
HttpDownloader::HttpDownloader()
{

}

HttpDownloader::~HttpDownloader()
{
}

//下载资源
//@params:url:需要下载的url
//@params:res_name:需要保存的资源名或位置,默认为执行目录下
//@return:false:失败, true:成功
bool HttpDownloader::DownloadRes(std::wstring url,std::wstring res_name/* =L"" */)
{
	URLDownloadToFile(nullptr,url.c_str(),res_name.c_str(),0,nullptr); //同步方式下载
	return true;
}

  • 测试使用
	unique_ptr<HttpDownloader> hd(new HttpDownloader());
	std::wstring demo = L"http://pic104.nipic.com/file/20160715/6171480_185807154956_2.jpg";
	std::wstring save_name = L"DENM.jpg";
	hd->DownloadRes(demo,save_name);
	system("pause");
发布了365 篇原创文章 · 获赞 80 · 访问量 35万+

猜你喜欢

转载自blog.csdn.net/Giser_D/article/details/103871960