MFC实现文件下载和上传


#include "stdafx.h"
#include "afxinet.h"
#include "afxwin.h"


#pragma comment(lib,"Wininet.lib")
BOOL UploadFile(LPCTSTR strURL, //负责接收上传操作的页面的URL
       LPCTSTR strLocalFileName)  //待上传的本地文件路径
{
 ASSERT(strURL != NULL && strLocalFileName != NULL);
 BOOL bResult = FALSE;
 DWORD dwType = 0;
 CString strServer;
 CString strObject;
 INTERNET_PORT wPort = 0;
 DWORD dwFileLength = 0;
 char * pFileBuff = NULL;


 CHttpConnection * pHC = NULL;
 CHttpFile * pHF = NULL;
 CInternetSession cis(_T("test"));


 bResult =  AfxParseURL(strURL, dwType, strServer, strObject, wPort);
 if(!bResult)
  return FALSE;
 CFile file;
 try
 {
  if(!file.Open(strLocalFileName, CFile::shareDenyNone | CFile::modeRead))
   return FALSE;
  dwFileLength = file.GetLength();
  if(dwFileLength <= 0)
   return FALSE;
  pFileBuff = new char[dwFileLength];
  memset(pFileBuff, 0, sizeof(char) * dwFileLength);
  file.Read(pFileBuff, dwFileLength);


  const int nTimeOut = 5000;
  cis.SetOption(INTERNET_OPTION_CONNECT_TIMEOUT, nTimeOut); //联接超时设置
  cis.SetOption(INTERNET_OPTION_CONNECT_RETRIES, 1);  //重试1次
  pHC = cis.GetHttpConnection(strServer, wPort);  //取得一个Http联接


  pHF = pHC->OpenRequest(CHttpConnection::HTTP_VERB_POST, strObject);
  {
   if(!pHF->SendRequest(NULL, 0, pFileBuff, dwFileLength))
    delete[]pFileBuff;
   pFileBuff = NULL;
   pHF->Close();
   pHC->Close();
   cis.Close();
   return FALSE;
  }
  DWORD dwStateCode = 0;
  pHF->QueryInfoStatusCode(dwStateCode);


  if(dwStateCode == HTTP_STATUS_OK)
   bResult = TRUE;
 }


 catch(CInternetException * pEx)
 {
  char sz[256] = "";
  pEx->GetErrorMessage(sz, 25);
  CString str;
  str.Format("InternetException occur!\r\n%s", sz);
  AfxMessageBox(str);
 }
 catch(CFileException& fe)
 {
  CString str;
  str.Format("FileException occur!\r\n%d", fe.m_lOsError);
  AfxMessageBox(str);
 }
 catch(...)
 {
  DWORD dwError = GetLastError();
  CString str;
  str.Format("Unknow Exception occur!\r\n%d", dwError);
  AfxMessageBox(str);
 }


 delete[]pFileBuff;
 pFileBuff = NULL;
 file.Close();
 pHF->Close();
 pHC->Close();
 cis.Close();
 return bResult;
}


BOOL Download(const CString& strFileURLInServer, //待下载文件的URL
    const CString & strFileLocalFullPath)//存放到本地的路径
{
 ASSERT(strFileURLInServer != "");
 ASSERT(strFileLocalFullPath != "");
 CInternetSession session(_T("test"));
 CHttpConnection* pHttpConnection = NULL;
 CHttpFile* pHttpFile = NULL;
 CString strServer, strObject;
 INTERNET_PORT wPort;


 DWORD dwType;
 const int nTimeOut = 2000;
 session.SetOption(INTERNET_OPTION_CONNECT_TIMEOUT, nTimeOut); //重试之间的等待延时
 session.SetOption(INTERNET_OPTION_CONNECT_RETRIES, 1);   //重试次数
 char* pszBuffer = NULL; 


 try
 {
  AfxParseURL(strFileURLInServer, dwType, strServer, strObject, wPort);
  pHttpConnection = session.GetHttpConnection(strServer, wPort);
  pHttpFile = pHttpConnection->OpenRequest(CHttpConnection::HTTP_VERB_GET, strObject);
  if(pHttpFile->SendRequest() == FALSE)
   return false;
  DWORD dwStateCode;


  pHttpFile->QueryInfoStatusCode(dwStateCode);
  if(dwStateCode == HTTP_STATUS_OK)
  {
   HANDLE hFile = CreateFile(strFileLocalFullPath, GENERIC_WRITE,
    FILE_SHARE_WRITE, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL,
    NULL);  //创建本地文件
   if(hFile == INVALID_HANDLE_VALUE)
   {
    pHttpFile->Close();
    pHttpConnection->Close();
    session.Close();
    return false;
   }


   char szInfoBuffer[1000];  //返回消息
   DWORD dwFileSize = 0;   //文件长度
   DWORD dwInfoBufferSize = sizeof(szInfoBuffer);
   BOOL bResult = FALSE;
   bResult = pHttpFile->QueryInfo(HTTP_QUERY_CONTENT_LENGTH,
    (void*)szInfoBuffer,&dwInfoBufferSize,NULL);


   dwFileSize = atoi(szInfoBuffer);
   const int BUFFER_LENGTH = 1024 * 10;
   pszBuffer = new char[BUFFER_LENGTH];  //读取文件的缓冲
   DWORD dwWrite, dwTotalWrite;
   dwWrite = dwTotalWrite = 0;
   UINT nRead = pHttpFile->Read(pszBuffer, BUFFER_LENGTH); //读取服务器上数据


   while(nRead > 0)
   {
    WriteFile(hFile, pszBuffer, nRead, &dwWrite, NULL);  //写到本地文件
    dwTotalWrite += dwWrite;
    nRead = pHttpFile->Read(pszBuffer, BUFFER_LENGTH);
   }


   delete[]pszBuffer;
   pszBuffer = NULL;
   CloseHandle(hFile);
  }
  else
  {
   delete[]pszBuffer;
   pszBuffer = NULL;
   if(pHttpFile != NULL)
   {
    pHttpFile->Close();
    delete pHttpFile;
    pHttpFile = NULL;
   }
   if(pHttpConnection != NULL)
   {
    pHttpConnection->Close();
    delete pHttpConnection;
    pHttpConnection = NULL;
   }
   session.Close();
   return false;
  }
 }
 catch(...)
 {
  delete[]pszBuffer;
  pszBuffer = NULL;
  if(pHttpFile != NULL)
  {
   pHttpFile->Close();
   delete pHttpFile;
   pHttpFile = NULL;
  }
  if(pHttpConnection != NULL)
  {
   pHttpConnection->Close();
   delete pHttpConnection;
   pHttpConnection = NULL;
  }
  session.Close();
  return false;
 }


 if(pHttpFile != NULL)
  pHttpFile->Close();
 if(pHttpConnection != NULL)
  pHttpConnection->Close();
 session.Close();
 return true;
}


//使用以上函数
int main(int argc, TCHAR* argv[])
{
 TCHAR szUrl[] = _T("http://10.20.115.68:8080/c/param.html?uuid=D30A75C8-03B6-4051-9A79-965A5398FEBA");
 Download(szUrl, _T("C:\\Users\\ZOUXIN\\Desktop\\http学习\\ttttt.txt"));
 return 0;
}

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
MFC(Microsoft Foundation Class)是一种使用C++编程语言开发的微软Windows平台下的应用程序框架。要在MFC实现窗口上文件的功能,可以按照以下步骤进行: 1. 创建一个MFC应用程序项目。 2. 在资源视图中添加一个按钮控件和一个编辑框控件,并为按钮添加一个点击事件的处理函数。 3. 在点击事件的处理函数中,使用CFileDialog类创建一个文件对话框,让用户选择要上文件。 4. 获取用户选择的文件路径并将其显示在编辑框中。 5. 使用CInternetSession类创建一个网络会话对象。 6. 使用CHttpConnection类创建一个HTTP请求对象,并指定上文件的URL地址。 7. 打开HTTP请求对象,并设置请求头、请求方法等参数。 8. 使用CInternetFile类创建一个上文件的流对象。 9. 打开上文件的流对象,并读取文件内容。 10. 将读取的文件内容通过HTTP请求对象发送到服务器。 11. 关闭上文件的流对象和HTTP请求对象。 12. 关闭网络会话对象。 13. 提示用户上文件成功。 以上是一个大致的实现过程,具体的代码实现涉及到MFC的各种类和函数的使用,需要根据具体的需求和情况进行调整和适配。在实际开发中,还需要考虑网络连接异常、文件读取错误等异常情况的处理。 总之,通过使用MFC中提供的网络相关类和文件操作类,可以方便地实现窗口上文件的功能。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值