void CtestDlg::OnBnClickedButton1()
{
// TODO: 在此添加控件通知处理程序代码
UpdateData(TRUE);
CString localFileName=_T("C:\\temp\\新建文本文档.txt");
CString webFileName=_T("http://6.1.90.33:7001//download//update//新建文本文档.txt")+pub_get_rand();
long trueFileSize=0;
DOWNFILE(localFileName,webFileName,trueFileSize);
UpdateData(FALSE);
}
void CtestDlg::UNICODE_TO_UTF8(CString pScoure,char *pDestion)
{
USES_CONVERSION;
LPWSTR pwStr=new wchar_t[pScoure.GetLength()+1];
wcscpy(pwStr,T2W((LPCTSTR)pScoure));
DWORD dwLength =WideCharToMultiByte(CP_UTF8,NULL,pwStr,-1,NULL,0,NULL,FALSE);
WideCharToMultiByte(CP_UTF8, 0, pwStr, -1, pDestion, dwLength,NULL,FALSE);
}
BOOL CtestDlg::DOWNFILE(CString localFileName,CString webFileName,long trueFileSize)
{
// if(trueFileSize!=0)
// {
// CFile file;
// if(file.Open(localFileName,CFile::modeRead))
// {
// ULONGLONG size= file.GetLength();
// if(trueFileSize==size)
// {
// file.Close();
// return TRUE;
// }
// }
// }
//
// if(trueFileSize==0)
// {
// CImage image;
// image.Load(localFileName);
// if(!image.IsNull())return TRUE;
// }
if(webFileName.GetLength()==0)return FALSE;
static char hex[]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
char *pString=new char[webFileName.GetLength()*sizeof(TCHAR)*3];
UNICODE_TO_UTF8(webFileName,pString);
int nLength=strlen(pString);
char pszEncode[2000];
ZeroMemory(pszEncode,2000);
int pos=0;
for( int i = 0; i < nLength; i++ )
{
unsigned char c = pString[i];
if( c > 0x20 && c < 0x7f ) // 数字或字母
{
pszEncode[pos] = c;
pos++;
}
else if( c == 0x20 ) // 包含空格
{
pszEncode[pos] = '+'; pos++;
}
else // 进行编码
{
pszEncode[pos] = '%'; pos++;
pszEncode[pos] = hex[c / 16]; pos++;
pszEncode[pos] = hex[c % 16]; pos++;
}
}
delete[]pString;
USES_CONVERSION;
DWORD re=URLDownloadToFile(NULL,A2T(pszEncode),(localFileName),0,NULL);
if (re== S_OK)
{
return TRUE;
}
else return FALSE;
}
CString CtestDlg::pub_get_rand()
{
CString strTime;
time_t nRand = time(NULL); // 用时间作为随机数
strTime.Format(_T("?time=%d"), nRand); // 生成随机URL
return strTime;
}