头文件
#pragma once
#include <atlstr.h>
#include <string>
using namespace std;
class CdownImg
{
public:
CString downPic(CString strImgUrl);
private:
CString downPicOld(CString strImgUrl);
CString CreateLocalPath(CString strImgUrl);
string AnsiToUtf8(LPCSTR Ansi);
};
cpp文件
#include "pch.h"
#include "CdownImg.h"
#include <wininet.h>
#include "io.h"
#include <direct.h>
#pragma comment(lib, "WININET.LIB")
#include <urlmon.h>
#pragma comment(lib, "urlmon.lib")
CString CdownImg::CreateLocalPath(CString strImgUrl)
{
CString filename = strImgUrl.Left(strImgUrl.ReverseFind('/'));
strImgUrl.Replace(filename + "/", "");
filename = strImgUrl;
CString folderpath = "./urlPic";
// 判断文件夹是否存在
if (_access(folderpath, 0) == -1)
{
int flag = _mkdir(folderpath);
if (0 != flag) {
return "";
}
}
CString str = folderpath + "/" + filename;
return str;
}
CString A2Wstr()
{
return "";
}
CString CdownImg::downPic(CString strImgUrl)
{
CString path = CreateLocalPath(strImgUrl);
if (_access(path, 0) != -1)
{
OutputDebugString("\nexists file:" + path);
return path;
}
char dir[1024] = "";
//1.转换绝对路径到dir
#ifdef _WIN32
_fullpath(dir, path, 1024);
#else
realpath(filePathbuf, dir);
#endif
size_t len = strImgUrl.GetLength();//line为string类型
int nmlen = MultiByteToWideChar(CP_ACP, 0, strImgUrl, len + 1, NULL, 0);
wchar_t *buffer = new wchar_t[nmlen];
MultiByteToWideChar(CP_ACP, 0, strImgUrl, len + 1, buffer, nmlen);
USES_CONVERSION;
HRESULT hr = URLDownloadToFileW(NULL, buffer, A2W(dir), 0, NULL);
delete[]buffer;
if (hr != NULL)
{
hr = URLDownloadToFile(NULL, AnsiToUtf8(strImgUrl).c_str(), dir, 0, NULL); //转码
if (hr != NULL)
{
downPicOld(strImgUrl); //再试一次
}
}
return path;
}
CString CdownImg::downPicOld(CString strImgUrl)
{
CString path = CreateLocalPath(strImgUrl);
if (_access(path, 0) != -1)
{
OutputDebugString("\nexists file:" + path);
return path;
}
DWORD length = 0;
BYTE buffer[1024];
memset(buffer, 0, 1024);
HINTERNET hInternet;
hInternet = InternetOpen(_T("Testing"), INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
if (hInternet == NULL)
{
return "";
}
HINTERNET hUrl;
hUrl = InternetOpenUrl(hInternet, AnsiToUtf8(strImgUrl).c_str(), NULL, 0, INTERNET_FLAG_RELOAD, 0);
if (hUrl == NULL)
{
InternetCloseHandle(hInternet);
return "";
}
BOOL hwrite;
DWORD written;
HANDLE hFile=NULL;
hFile = CreateFile(path, GENERIC_WRITE, 0, 0, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
if (hFile == INVALID_HANDLE_VALUE)
{
InternetCloseHandle(hUrl);
InternetCloseHandle(hInternet);
return "";
}
BOOL read;
while (TRUE)
{
read = InternetReadFile(hUrl, buffer, sizeof(buffer), &length);
if (length == 0)
break;
hwrite = WriteFile(hFile, buffer, sizeof(buffer), &written, NULL);
if (hwrite == 0)
{
CloseHandle(hFile);
InternetCloseHandle(hUrl);
InternetCloseHandle(hInternet);
return "";
}
}
CloseHandle(hFile);
InternetCloseHandle(hUrl);
InternetCloseHandle(hInternet);
return path;
}
string CdownImg::AnsiToUtf8(LPCSTR Ansi)
{
int WLength = MultiByteToWideChar(CP_ACP, 0, Ansi, -1, NULL, 0);
LPWSTR pszW = (LPWSTR)malloc((WLength + 1) * sizeof(WCHAR));
MultiByteToWideChar(CP_ACP, 0, Ansi, -1, pszW, WLength);
int ALength = WideCharToMultiByte(CP_UTF8, 0, pszW, -1, NULL, 0, NULL, NULL);
LPSTR pszA = (LPSTR)malloc(ALength + 1);
WideCharToMultiByte(CP_UTF8, 0, pszW, -1, pszA, ALength, NULL, NULL);
pszA[ALength] = '\0';
string rst(pszA);
free(pszW);
free(pszA);
return rst;
}
使用:
CString ts = “http://localhost:8021/floorImg/s中国红.png”;
CdownImg down;
down.downPic(ts);
ts = “http://localhost:8021/floorImg/s中国.png”;
down.downPic(ts);
注:
InternetReadFile 对路径中的中文个数是奇数无法读取
URLDownloadToFileW 需要使用宽字节