大瓜子 2016-10-21 01:37 采纳率: 0%
浏览 2650
已结题

URLDownloadToFile下载 中文文件失败

 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;
}

  • 写回答

3条回答 默认 最新

  • threenewbee 2016-10-21 01:42
    关注
    评论

报告相同问题?

悬赏问题

  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大