GSI

원격지에 있는 파일의 사이즈 체크 하는 방법

DWORD GetUrlFileLength (CString url)
{
 DWORD filesize;

 TCHAR   szCause[255];
 CString CauseOfError;
 TRY
 {
  CInternetSession session;
  CHttpFile *remotefile= (CHttpFile*)session.OpenURL(url,1,INTERNET_FLAG_TRANSFER_BINARY);
  TCHAR szContentLength[32];
  DWORD dwInfoSize = 32;
  DWORD dwFileSize = 0;
  BOOL bGotFileSize = FALSE;
  if (remotefile->QueryInfo ( HTTP_QUERY_CONTENT_LENGTH, szContentLength, &dwInfoSize , NULL))
  {
   bGotFileSize = TRUE;
   dwFileSize = (DWORD) _ttol(szContentLength);
   filesize = dwFileSize;// Return 값에 할당.
  }
  else
  { // 에러나 나서 파일이 없을경우에
   filesize = -1 ;
  }
  remotefile->Close ();
  session.Close ();
  delete remotefile;
  delete session;
 }

 CATCH_ALL(error)
 {
  AfxMessageBox ("서버로부터 파일 크기를 얻어오는 과정에서 에러발생");
  error->GetErrorMessage(szCause,254,NULL);
  CauseOfError.Format("%s",szCause);
  AfxMessageBox (CauseOfError);
 }
 END_CATCH_ALL;

 return filesize;
}

원격지에 있는 파일 다운로드 받기

void DownloadUrlFileBuffer (CString url)
{
 DWORD dwServiceType = AFX_INET_SERVICE_HTTP;
 CString szServer, szObject, szInfo;

 INTERNET_PORT nPort;
 INTERNET_PROXY_INFO m_proxyinfo;

 CInternetSession m_SessionDownload;
 CHttpConnection* m_pConnection = NULL;
 CHttpFile* m_pHttpFile = NULL;
 CFile FileWrite;

 DWORD d_BytesRead=0;
 DWORD d_FileSize=0;

 char szHTTPBuffer[199926];

 ZeroMemory(szHTTPBuffer, sizeof(szHTTPBuffer));

 //start Download Routine
 ::AfxParseURL(url.GetBuffer(url.GetLength()), dwServiceType, szServer, szObject, nPort);

 try
 {
  m_pConnection = m_SessionDownload.GetHttpConnection(szServer,INTERNET_FLAG_KEEP_CONNECTION, nPort,NULL, NULL);
  m_pHttpFile= m_pConnection->OpenRequest("GET",szObject,NULL,0, NULL, NULL, INTERNET_FLAG_KEEP_CONNECTION);
 }
 catch(CInternetException* m_pException)
 {
  //exception found
  //lots of clean up code
  return;
 }

 if(m_pHttpFile)
 {
  if(!FileWrite.Open("d:\\aaa.gif", CFile::modeCreate | CFile::modeReadWrite | CFile::shareDenyNone ))
  {
   //exception found
   //lots of clean up code
   return;
  }

  try
  {
   m_pHttpFile->SendRequest(NULL);
  }
  catch(CInternetException* m_pException)
  {
   //exception found
   //lots of clean up code
   return;
  }

  m_pHttpFile->QueryInfo(HTTP_QUERY_CONTENT_LENGTH, d_FileSize);
  m_pHttpFile->QueryInfo(HTTP_QUERY_MAX, szInfo);

  while(d_BytesRead = m_pHttpFile->Read((void*)szHTTPBuffer,199926))
  {
   FileWrite.Write((void *)szHTTPBuffer,d_BytesRead);
   memset(szHTTPBuffer, 0, sizeof(szHTTPBuffer));
  }

  szServer.Empty();
  szObject.Empty();

  m_SessionDownload.Close();
  m_pConnection->Close();
  m_pHttpFile->Close();

  delete m_SessionDownload;
  delete m_pConnection;
  delete m_pHttpFile;

  FileWrite.Close();
 }
}

위에 있는 FileWrite 부분이 그 부분이며, 이 부분을 해당 프로젝트에 맞도록 고쳐야 할 것이다.
Posted by gsi
: