123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189 |
- #include "StdAfx.h"
- #include "DnowLoadImg.h"
- #include <afxinet.h>
- #include "basic_struct.h"
- #include <codecvt>
- #include "ServerConfig.h"
- #include "MyFunction.h"
- #include "..\EvaluationUtil\HttpClient.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- void DeleteDirectory(CString source)
- {
- //AfxMessageBox("创建文件夹"+target);
- CFileFind finder;
- CString path;
- path.Format(_T("%s/*.*"),source);
- bool bWorking = finder.FindFile(path);
- while(bWorking){
- bWorking = finder.FindNextFile();
- if(finder.IsDirectory() && !finder.IsDots()){ //是文件夹 而且 名称不含 . 或 ..
- DeleteDirectory(finder.GetFilePath()); //递归创建文件夹+"/"+finder.GetFileName()
- }
- else{ //是文件 则直接删除
- DeleteFile(finder.GetFilePath());
- }
- }
- RemoveDirectory(source);
- }
- boost::shared_ptr<CDnowLoadImg> CDnowLoadImg::GetInstance()
- {
- static boost::shared_ptr<CDnowLoadImg> _instance(new CDnowLoadImg);
- return _instance;
- }
- CDnowLoadImg::CDnowLoadImg(void)
- {
- m_hwnd =NULL;
- TCHAR FilePath[MAX_PATH];
- GetModuleFileName(NULL, FilePath, MAX_PATH);
- (_tcsrchr(FilePath, '\\'))[1] = 0;
- lstrcat(FilePath, _T("config.ini"));
- WCHAR URL[512];
- GetPrivateProfileString(_T("GLOBAL"), _T("server_url"), _T("http://106.75.2.185/netmark"), URL, 512, FilePath);
- m_url.Format(_T("%s/api/getTaskImg"), URL);
- }
- CDnowLoadImg::~CDnowLoadImg(void)
- {
- }
- ServiceState CDnowLoadImg::OnStarting( void )
- {
- TCHAR exeFullPath[MAX_PATH];
- CString strPath;
- GetModuleFileName(NULL,exeFullPath,MAX_PATH);
- strPath=(CString)exeFullPath;
- int position=strPath.ReverseFind('\\');
- strPath=strPath.Left(position+1);
- strPath += "imge";
- CreateDirectory(strPath, NULL);
- strPath += "\\";
- m_filepath = strPath;
-
- return running;
- }
- ServiceState CDnowLoadImg::OnRunning( void )
- {
- m_flag = GetImgePath();
- if (m_flag)
- {
- m_flag = DownImge();
- }
- return stoping;
- }
- ServiceState CDnowLoadImg::OnStoping( void )
- {
- if (m_hwnd != NULL)
- {
- SendMessage(m_hwnd, WM_IMG_DOWNLOAD_OVER, (WPARAM)m_flag, (LPARAM)(&m_filelistout));
- }
- return IService::OnStoping();
- }
- void CDnowLoadImg::SetDownFileList(const std::vector<std::string> &downfilelist)
- {
- m_downfilelist = downfilelist;
- }
- bool CDnowLoadImg::GetImgePath()
- {
- bool flag = false;
- CHttpClient httpClient;
- //httpClient.SetSendHeader(MarkHttpHeaderData());
- CString postData;
- CString ff;
- ff = CA2T(m_strExamId.c_str());
- postData.Format(_T("exam_group_id=%s&type=%d"), ff, m_type);
- Json::Features features;
- Json::Reader reader(features);
- string response;
- int ret = httpClient.HttpPost(m_url, postData, response);
- if (ret != 0)
- {
- return false;
- }
- else
- {
- Json::Value root;
- reader.parse(response, root);
- if (root["status"].isInt() && root["status"].asInt()){
- Json::Value data = root["data"];
- for (int i = 0; i < data.size(); i++)
- {
- m_downfilelist.push_back(data[i].asString().c_str());
- flag = true;
- }
- }
- }
- return flag;
- }
- bool CDnowLoadImg::DownImge()
- {
- bool flag = true;
- std::vector<char> data;
- CHttpClient httpClient;
- for (int i = 1; i <= m_downfilelist.size(); i++)
- {
- std::vector<char> data;
- if (httpClient.HttpDownload(CA2T(m_downfilelist[i - 1].c_str()), NULL, data) == 0)
- {
- CFile file;
- CString fliepath;
- {
- CString temp;
- temp.Format(_T("imge%d.jpg"), i);
- fliepath = m_filepath + temp;
- }
-
- file.Open(fliepath, CFile::modeCreate | CFile::modeWrite);
- file.Write(data.data(), data.size());
- file.Close();
- std::string temp = CT2A(fliepath.GetBuffer());
- m_filelistout.push_back(temp);
- }
- else
- {
- flag = false;
- break;
- }
-
- }
- return flag;
- }
- CString CDnowLoadImg::MarkHttpHeaderData()
- {
- TCHAR FilePath[MAX_PATH];
- GetModuleFileName(NULL, FilePath, MAX_PATH);
- (_tcsrchr(FilePath, '\\'))[1] = 0;
- lstrcat(FilePath, _T("config.ini"));
- #ifndef BACK_STAGE
- WCHAR SIG[512];
- GetPrivateProfileString(_T("SESSIONID"), _T("SIG"), _T(""), SIG, 512, FilePath);
- WCHAR TIME[512];
- GetPrivateProfileString(_T("SESSIONID"), _T("TIME"), _T(""), TIME, 512, FilePath);
- WCHAR ID[512];
- GetPrivateProfileString(L"SESSIONID", L"ID", L"", ID, 512, FilePath);
- WCHAR JSESSIONID[512];
- GetPrivateProfileString(_T("SESSIONID"), _T("JSESSIONID"), _T(""), JSESSIONID, 512, FilePath);
- CString ret;
- ret.Format(_T("userid=%s&time=%s&sig=%s&session=%s"), ID, TIME, SIG, JSESSIONID);
- #else
- WCHAR MYCOOK[512];
- GetPrivateProfileString(_T("SESSIONID"), _T("MYCOOK"), _T(""), MYCOOK, 512, FilePath);
- CString ret;
- ret.Format(_T("%s"), MYCOOK);
- #endif
- return ret;
- }
|