DnowLoadImg.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. #include "StdAfx.h"
  2. #include "DnowLoadImg.h"
  3. #include <afxinet.h>
  4. #include "basic_struct.h"
  5. #include <codecvt>
  6. #include "ServerConfig.h"
  7. #include "MyFunction.h"
  8. #include "..\EvaluationUtil\HttpClient.h"
  9. #ifdef _DEBUG
  10. #define new DEBUG_NEW
  11. #undef THIS_FILE
  12. static char THIS_FILE[] = __FILE__;
  13. #endif
  14. void DeleteDirectory(CString source)
  15. {
  16. //AfxMessageBox("创建文件夹"+target);
  17. CFileFind finder;
  18. CString path;
  19. path.Format(_T("%s/*.*"),source);
  20. bool bWorking = finder.FindFile(path);
  21. while(bWorking){
  22. bWorking = finder.FindNextFile();
  23. if(finder.IsDirectory() && !finder.IsDots()){ //是文件夹 而且 名称不含 . 或 ..
  24. DeleteDirectory(finder.GetFilePath()); //递归创建文件夹+"/"+finder.GetFileName()
  25. }
  26. else{ //是文件 则直接删除
  27. DeleteFile(finder.GetFilePath());
  28. }
  29. }
  30. RemoveDirectory(source);
  31. }
  32. boost::shared_ptr<CDnowLoadImg> CDnowLoadImg::GetInstance()
  33. {
  34. static boost::shared_ptr<CDnowLoadImg> _instance(new CDnowLoadImg);
  35. return _instance;
  36. }
  37. CDnowLoadImg::CDnowLoadImg(void)
  38. {
  39. m_hwnd =NULL;
  40. TCHAR FilePath[MAX_PATH];
  41. GetModuleFileName(NULL, FilePath, MAX_PATH);
  42. (_tcsrchr(FilePath, '\\'))[1] = 0;
  43. lstrcat(FilePath, _T("config.ini"));
  44. WCHAR URL[512];
  45. GetPrivateProfileString(_T("GLOBAL"), _T("server_url"), _T("http://106.75.2.185/netmark"), URL, 512, FilePath);
  46. m_url.Format(_T("%s/api/getTaskImg"), URL);
  47. }
  48. CDnowLoadImg::~CDnowLoadImg(void)
  49. {
  50. }
  51. ServiceState CDnowLoadImg::OnStarting( void )
  52. {
  53. TCHAR exeFullPath[MAX_PATH];
  54. CString strPath;
  55. GetModuleFileName(NULL,exeFullPath,MAX_PATH);
  56. strPath=(CString)exeFullPath;
  57. int position=strPath.ReverseFind('\\');
  58. strPath=strPath.Left(position+1);
  59. strPath += "imge";
  60. CreateDirectory(strPath, NULL);
  61. strPath += "\\";
  62. m_filepath = strPath;
  63. return running;
  64. }
  65. ServiceState CDnowLoadImg::OnRunning( void )
  66. {
  67. m_flag = GetImgePath();
  68. if (m_flag)
  69. {
  70. m_flag = DownImge();
  71. }
  72. return stoping;
  73. }
  74. ServiceState CDnowLoadImg::OnStoping( void )
  75. {
  76. if (m_hwnd != NULL)
  77. {
  78. SendMessage(m_hwnd, WM_IMG_DOWNLOAD_OVER, (WPARAM)m_flag, (LPARAM)(&m_filelistout));
  79. }
  80. return IService::OnStoping();
  81. }
  82. void CDnowLoadImg::SetDownFileList(const std::vector<std::string> &downfilelist)
  83. {
  84. m_downfilelist = downfilelist;
  85. }
  86. bool CDnowLoadImg::GetImgePath()
  87. {
  88. bool flag = false;
  89. CHttpClient httpClient;
  90. //httpClient.SetSendHeader(MarkHttpHeaderData());
  91. CString postData;
  92. CString ff;
  93. ff = CA2T(m_strExamId.c_str());
  94. postData.Format(_T("exam_group_id=%s&type=%d"), ff, m_type);
  95. Json::Features features;
  96. Json::Reader reader(features);
  97. string response;
  98. int ret = httpClient.HttpPost(m_url, postData, response);
  99. if (ret != 0)
  100. {
  101. return false;
  102. }
  103. else
  104. {
  105. Json::Value root;
  106. reader.parse(response, root);
  107. if (root["status"].isInt() && root["status"].asInt()){
  108. Json::Value data = root["data"];
  109. for (int i = 0; i < data.size(); i++)
  110. {
  111. m_downfilelist.push_back(data[i].asString().c_str());
  112. flag = true;
  113. }
  114. }
  115. }
  116. return flag;
  117. }
  118. bool CDnowLoadImg::DownImge()
  119. {
  120. bool flag = true;
  121. std::vector<char> data;
  122. CHttpClient httpClient;
  123. for (int i = 1; i <= m_downfilelist.size(); i++)
  124. {
  125. std::vector<char> data;
  126. if (httpClient.HttpDownload(CA2T(m_downfilelist[i - 1].c_str()), NULL, data) == 0)
  127. {
  128. CFile file;
  129. CString fliepath;
  130. {
  131. CString temp;
  132. temp.Format(_T("imge%d.jpg"), i);
  133. fliepath = m_filepath + temp;
  134. }
  135. file.Open(fliepath, CFile::modeCreate | CFile::modeWrite);
  136. file.Write(data.data(), data.size());
  137. file.Close();
  138. std::string temp = CT2A(fliepath.GetBuffer());
  139. m_filelistout.push_back(temp);
  140. }
  141. else
  142. {
  143. flag = false;
  144. break;
  145. }
  146. }
  147. return flag;
  148. }
  149. CString CDnowLoadImg::MarkHttpHeaderData()
  150. {
  151. TCHAR FilePath[MAX_PATH];
  152. GetModuleFileName(NULL, FilePath, MAX_PATH);
  153. (_tcsrchr(FilePath, '\\'))[1] = 0;
  154. lstrcat(FilePath, _T("config.ini"));
  155. #ifndef BACK_STAGE
  156. WCHAR SIG[512];
  157. GetPrivateProfileString(_T("SESSIONID"), _T("SIG"), _T(""), SIG, 512, FilePath);
  158. WCHAR TIME[512];
  159. GetPrivateProfileString(_T("SESSIONID"), _T("TIME"), _T(""), TIME, 512, FilePath);
  160. WCHAR ID[512];
  161. GetPrivateProfileString(L"SESSIONID", L"ID", L"", ID, 512, FilePath);
  162. WCHAR JSESSIONID[512];
  163. GetPrivateProfileString(_T("SESSIONID"), _T("JSESSIONID"), _T(""), JSESSIONID, 512, FilePath);
  164. CString ret;
  165. ret.Format(_T("userid=%s&time=%s&sig=%s&session=%s"), ID, TIME, SIG, JSESSIONID);
  166. #else
  167. WCHAR MYCOOK[512];
  168. GetPrivateProfileString(_T("SESSIONID"), _T("MYCOOK"), _T(""), MYCOOK, 512, FilePath);
  169. CString ret;
  170. ret.Format(_T("%s"), MYCOOK);
  171. #endif
  172. return ret;
  173. }