lhfileremote.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756
  1. #include "lhfileremote.h"
  2. #include <QStringList>
  3. #include <QFile>
  4. #include <QUrl>
  5. #include <QDebug>
  6. #include "curl.h"
  7. #include "Common/External/Core/WNetUtility/wnetutility.h"
  8. long* g_pPos = NULL;
  9. qint64 g_llTotal =0;
  10. qint64 g_llTotalWrite=0;
  11. //2016.12.8 url 解码
  12. #define IsHexNum(c) ((c >= '0' && c <= '9') || (c >= 'A' && c <= 'F') || (c >= 'a' && c <= 'f'))
  13. QString UrlDecode(const QString& url)
  14. {
  15. return QUrl::toPercentEncoding(url);
  16. }
  17. QString UrlEncodeEx(const QString& strToEncode)
  18. {
  19. return QUrl::fromPercentEncoding(strToEncode.toUtf8());
  20. }
  21. CLHFileRemote::CLHFileRemote(void)
  22. {
  23. m_btCurlPASV = 0;
  24. }
  25. CLHFileRemote::~CLHFileRemote(void)
  26. {
  27. }
  28. bool CLHFileRemote::LoginServer(const QString& lpszPath, const QString& lpszUser, const QString& lpszPwd)
  29. {
  30. QString strPath(lpszPath);
  31. strPath.replace("\\\\", ";;");
  32. QStringList ayRet = strPath.split("\\");
  33. if( !ayRet.isEmpty() )
  34. {
  35. strPath = ayRet.at(0);
  36. }
  37. strPath.replace(";;", "\\\\");
  38. bool bNetWork = (strPath.indexOf("\\\\")==0);
  39. if( bNetWork && lpszUser.size() > 0 )
  40. { // 网络路径
  41. if(QFile::exists(lpszPath))
  42. {
  43. return true;
  44. }
  45. bool bRet = false;
  46. #ifdef Q_OS_WIN
  47. bRet = WNetUtility::WNetTryConnect(lpszPwd, lpszUser, lpszPwd);
  48. #endif
  49. return bRet ? true : false;
  50. }
  51. else
  52. { // 本机路径
  53. if( QFile::exists(lpszPath) )
  54. {
  55. return false;
  56. }
  57. return true;
  58. }
  59. }
  60. //UTF8 TO GBK
  61. //QString CLHFileRemote::UTF8ToGBK(QString str)
  62. //{
  63. // int len=MultiByteToWideChar(CP_UTF8, 0, (LPCTSTR)str, -1, NULL,0);
  64. // WCHAR * wszGBK = new WCHAR[len+1];
  65. // memset(wszGBK, 0, len * 2 + 2);
  66. // MultiByteToWideChar(CP_UTF8, 0, (LPCTSTR)str, -1, wszGBK, len);
  67. // len = WideCharToMultiByte(CP_ACP, 0, wszGBK, -1, NULL, 0, NULL, NULL);
  68. // char *szGBK=new char[len + 1];
  69. // memset(szGBK, 0, len + 1);
  70. // WideCharToMultiByte (CP_ACP, 0, wszGBK, -1, szGBK, len, NULL,NULL);
  71. // QString strResult = szGBK;
  72. // delete[] szGBK;
  73. // delete[] wszGBK;
  74. // return strResult;
  75. //}
  76. //GBK TO UTF8
  77. //QString CLHFileRemote::GBKToUTF8(QString& str)
  78. //{
  79. // int len=MultiByteToWideChar(CP_ACP, 0, (LPCTSTR)str, -1, NULL,0);
  80. // WCHAR * wszUtf8 = new WCHAR[len*2];
  81. // memset(wszUtf8, 0, len * 4);
  82. // MultiByteToWideChar(CP_ACP, 0, (LPCTSTR)str, -1, wszUtf8, len);
  83. // len = WideCharToMultiByte(CP_UTF8, 0, wszUtf8, -1, NULL, 0, NULL, NULL);
  84. // char *szUtf8=new char[len *2];
  85. // memset(szUtf8, 0, len *2);
  86. // WideCharToMultiByte (CP_UTF8, 0, wszUtf8, -1, szUtf8, len, NULL,NULL);
  87. // QString strResult = szUtf8;
  88. // delete[] szUtf8;
  89. // delete[] wszUtf8;
  90. // return strResult;
  91. //}
  92. size_t write_data_post( void *buffer, size_t size, size_t nmemb, void *user_p )
  93. {
  94. Q_UNUSED(size);
  95. SUsreDate* ud = (SUsreDate*)user_p;
  96. if(ud)
  97. {
  98. CLHFileRemote* pObject = (CLHFileRemote*)ud->pOwner;
  99. ud->buffer = (char*)buffer;
  100. ud->buffersize = nmemb;
  101. return pObject->curl_ReceivePost(ud);
  102. }
  103. return 0;
  104. }
  105. static size_t throw_away(void *ptr, size_t size,size_t nmemb,void *data)
  106. {
  107. (void)ptr;
  108. (void)data;
  109. return (size_t)(size * nmemb);
  110. }
  111. size_t read_data( void *buffer, size_t size, size_t nmemb, void *user_p )
  112. {
  113. return fread(buffer, size, nmemb, (FILE *)user_p);
  114. }
  115. size_t write_data( void *buffer, size_t size, size_t nmemb, void *user_p )
  116. {
  117. SUpDownData* pData = (SUpDownData*)user_p;
  118. if( pData)
  119. {
  120. if( pData->llTotal>0)
  121. *(pData->nProgress) = ( (pData->llBaseSize + pData->llCurrent)*100)/pData->llTotal;
  122. size_t nSize=size*nmemb;
  123. if( pData->pFile)
  124. {
  125. nSize = fwrite(buffer, size, nmemb, (FILE *)pData->pFile);
  126. }
  127. else
  128. {
  129. if( pData->pBuffer)
  130. memcpy(pData->pBuffer+pData->llCurrent,buffer,nSize);
  131. }
  132. pData->llCurrent += size*nmemb;
  133. return nSize;
  134. }
  135. return 0;
  136. }
  137. size_t CLHFileRemote::curl_ReceivePost(SUsreDate* ud)
  138. {
  139. if( ud )
  140. {
  141. char* pBuffer = new char[ud->buffersize+1];
  142. memset(pBuffer,0, ud->buffersize+1 );
  143. memcpy(pBuffer,ud->buffer,ud->buffersize);
  144. QString strReceive(pBuffer);
  145. QString strGBK = strReceive;// utf8->gbk?
  146. delete[] pBuffer;
  147. pBuffer =NULL;
  148. pBuffer = new char [strGBK.length()+1];
  149. memset(pBuffer,0, strGBK.length()+1 );
  150. sprintf(pBuffer,"%s",strGBK.toStdString().c_str());
  151. if( strGBK.length() >100)
  152. m_strErrorPostContent = strGBK.left(100);
  153. else
  154. m_strErrorPostContent = strGBK;
  155. int size = ud->buffersize;
  156. delete ud;
  157. ud = NULL;
  158. return size;
  159. }
  160. return 0;
  161. }
  162. bool CLHFileRemote::curl_Post(const QString& strUrl,const QString& strContent)
  163. {
  164. SUsreDate* ud = new SUsreDate;
  165. ud->pOwner = this;
  166. ud->strContent = strContent;
  167. ud->btRequestType = 0;
  168. m_strErrorPostContent = "";
  169. CURL *curl_easy_handle = curl_easy_init();
  170. if (NULL == curl_easy_handle)
  171. {
  172. m_strError = "CURL初始化失败";
  173. return false;
  174. }
  175. // 将路径转换为url编码格式
  176. QString strPath = strUrl;
  177. strPath = UrlDecode(strPath);
  178. strPath = UrlEncodeEx(strPath);
  179. //strContent = strContent;// gbk->utf8
  180. int nLen = strContent.length()+1;
  181. char* szContent = new char[nLen];
  182. memset(szContent,0,nLen);
  183. sprintf(szContent,"%s",strContent.toStdString().c_str());
  184. //设置URL
  185. curl_easy_setopt(curl_easy_handle, CURLOPT_URL, strPath.toStdString().c_str());
  186. // 设置http发送的内容类型为JSON
  187. curl_slist *plist = curl_slist_append(NULL,"Content-Type:application/x-www-form-urlencoded;charset=utf-8"); //text/html; //Content-Type:application/json;
  188. curl_easy_setopt(curl_easy_handle, CURLOPT_HTTPHEADER, plist);
  189. // 设置http发送的内容
  190. curl_easy_setopt(curl_easy_handle, CURLOPT_POSTFIELDS,szContent);
  191. //设置返回数据
  192. curl_easy_setopt(curl_easy_handle,CURLOPT_WRITEFUNCTION,write_data_post); //对返回的数据进行操作的函数地址
  193. curl_easy_setopt(curl_easy_handle,CURLOPT_WRITEDATA,ud); //这是write_data的第四个参数值
  194. //设置其他
  195. curl_easy_setopt(curl_easy_handle, CURLOPT_TIMEOUT, 15);
  196. curl_easy_setopt(curl_easy_handle, CURLOPT_TCP_KEEPALIVE, 1);
  197. // 执行创建操作
  198. CURLcode res = curl_easy_perform(curl_easy_handle);
  199. if (res != CURLE_OK)
  200. {
  201. if( res != CURLE_WRITE_ERROR)
  202. {
  203. m_strError = QString("CURL执行失败(%1),%2").arg(res).arg(curl_easy_strerror(res));
  204. }
  205. curl_easy_cleanup(curl_easy_handle);
  206. return false;
  207. }
  208. curl_easy_cleanup(curl_easy_handle);
  209. return true;
  210. }
  211. bool CLHFileRemote::curl_IsFileExist(const QString& strRemoteFile)
  212. {
  213. CURL *curl;
  214. CURLcode res;
  215. long filetime = -1;
  216. double filesize = 0.0;
  217. curl = curl_easy_init();
  218. if(curl)
  219. {
  220. curl_easy_setopt(curl, CURLOPT_URL, strRemoteFile.toStdString().c_str());
  221. curl_easy_setopt(curl, CURLOPT_NOBODY, 1L);
  222. curl_easy_setopt(curl, CURLOPT_FILETIME, 1L);
  223. curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, throw_away);
  224. curl_easy_setopt(curl, CURLOPT_HEADER, 0L);
  225. res = curl_easy_perform(curl);
  226. if(CURLE_OK == res)
  227. {
  228. res = curl_easy_getinfo(curl, CURLINFO_FILETIME, &filetime);
  229. if((CURLE_OK == res) && (filetime >= 0))
  230. {
  231. //time_t file_time = (time_t)filetime;
  232. return true;
  233. }
  234. res = curl_easy_getinfo(curl, CURLINFO_CONTENT_LENGTH_DOWNLOAD, &filesize);
  235. if((CURLE_OK == res) && (filesize>0.0))
  236. {
  237. return true;
  238. }
  239. }
  240. else
  241. {
  242. fprintf(stderr, "curl told us %d\n", res);
  243. }
  244. curl_easy_cleanup(curl);
  245. }
  246. return false;
  247. }
  248. /**
  249. * @brief FTP上传时,root用户貌似不行 2024-01-06
  250. * @param szLocalFile
  251. * @param szRemoteFile
  252. * @param szUser
  253. * @param szPwd
  254. * @return
  255. */
  256. bool CLHFileRemote::curl_Upload(const char* szLocalFile, const char* szRemoteFile,const char* szUser,const char* szPwd)
  257. {
  258. // 获取curl句柄
  259. CURL *curl_easy_handle = curl_easy_init();
  260. if (NULL == curl_easy_handle)
  261. {
  262. m_strError = "CURL初始化失败";
  263. return false;
  264. }
  265. // 打开本地文件
  266. FILE *fp = NULL;
  267. fp = fopen(szLocalFile, "ab+");
  268. if (NULL == fp)
  269. {
  270. curl_easy_cleanup(curl_easy_handle);
  271. m_strError = QString("上载失败,打开本地文件失败:%1").arg(szLocalFile);
  272. return false;
  273. }
  274. // 获取文件大小
  275. fseek(fp, 0, SEEK_END);
  276. long file_size = ftell(fp);
  277. rewind(fp);
  278. // 将路径转换为url编码格式,ftp模式是不需要的(2024.01.06)
  279. QString strPath(szRemoteFile);
  280. strPath = UrlDecode(strPath);
  281. if(strlen(szUser)>0)
  282. {
  283. curl_easy_setopt(curl_easy_handle, CURLOPT_USERNAME, szUser);
  284. curl_easy_setopt(curl_easy_handle, CURLOPT_PASSWORD, szPwd);
  285. }
  286. if( strPath.left(4).compare("http", Qt::CaseInsensitive) == 0 )
  287. {
  288. curl_easy_setopt(curl_easy_handle, CURLOPT_POST, 1L);
  289. curl_easy_setopt(curl_easy_handle, CURLOPT_URL, strPath.toLocal8Bit().data());
  290. curl_easy_setopt(curl_easy_handle, CURLOPT_READFUNCTION, &read_data);
  291. curl_easy_setopt(curl_easy_handle, CURLOPT_READDATA, fp);
  292. curl_easy_setopt(curl_easy_handle, CURLOPT_FAILONERROR, 1);
  293. }
  294. else
  295. {
  296. curl_easy_setopt(curl_easy_handle, CURLOPT_UPLOAD, 1);
  297. curl_easy_setopt(curl_easy_handle, CURLOPT_URL, szRemoteFile);
  298. curl_easy_setopt(curl_easy_handle, CURLOPT_READFUNCTION, &read_data);
  299. curl_easy_setopt(curl_easy_handle, CURLOPT_READDATA, fp);
  300. curl_easy_setopt(curl_easy_handle, CURLOPT_INFILESIZE, file_size);
  301. curl_easy_setopt(curl_easy_handle, CURLOPT_FAILONERROR, 1);
  302. }
  303. // 执行上传操作
  304. CURLcode res = curl_easy_perform(curl_easy_handle);
  305. // ftp协议返回码
  306. long retcode = 0;
  307. curl_easy_getinfo(curl_easy_handle, CURLINFO_RESPONSE_CODE, &retcode);
  308. // 释放资源
  309. fclose(fp);
  310. curl_easy_cleanup(curl_easy_handle);
  311. if (res != CURLE_OK)
  312. {
  313. m_strError = QString("上载失败,返回码:%1,%2").arg(retcode).arg(res);
  314. return false;
  315. }
  316. return true;
  317. }
  318. qint64 CLHFileRemote::curl_GetFileLength(const char* szRemoteFile,const char* szUser,const char* szPwd)
  319. {
  320. // 将路径转换为url编码格式
  321. QString strPath(szRemoteFile);
  322. //2017.9.30
  323. //strPath = strPath;// gbk->utf8
  324. strPath = UrlEncodeEx(strPath);
  325. if( strPath.left(4).compare("http", Qt::CaseInsensitive) != 0 &&
  326. strPath.left(3).compare("ftp", Qt::CaseInsensitive) !=0 )
  327. {
  328. //qint64 nReadTotal = 0;
  329. strPath.replace("/","\\");
  330. LoginServer(strPath.toStdString().c_str(),szUser,szPwd);
  331. QFile fStatus(strPath);
  332. if( !fStatus.open(QIODevice::ReadOnly))
  333. {
  334. return -1;
  335. }
  336. return fStatus.size();
  337. }
  338. // 获取curl句柄
  339. CURL *curl_easy_handle = curl_easy_init();
  340. if (NULL == curl_easy_handle)
  341. {
  342. m_strError = "CURL初始化失败";
  343. return -1;
  344. }
  345. if( strPath.left(5).compare("https", Qt::CaseInsensitive) == 0)
  346. {
  347. //curl_easy_setopt(curl_easy_handle, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V6);
  348. curl_easy_setopt(curl_easy_handle, CURLOPT_SSL_VERIFYPEER, FALSE); // 跳过证书检查
  349. curl_easy_setopt(curl_easy_handle, CURLOPT_SSL_VERIFYHOST, FALSE); // 从证书中检查SSL加密算法是否存在
  350. }
  351. // 设置curl_easy_handle属性
  352. if(strlen(szUser)>0)
  353. {
  354. curl_easy_setopt(curl_easy_handle, CURLOPT_USERNAME, szUser);
  355. curl_easy_setopt(curl_easy_handle, CURLOPT_PASSWORD, szPwd);
  356. }
  357. curl_easy_setopt(curl_easy_handle, CURLOPT_URL, strPath.toStdString().c_str());
  358. curl_easy_setopt(curl_easy_handle, CURLOPT_NOBODY, 1L);
  359. curl_easy_setopt(curl_easy_handle, CURLOPT_FILETIME, 1L);
  360. curl_easy_setopt(curl_easy_handle, CURLOPT_HEADERFUNCTION, throw_away);
  361. curl_easy_setopt(curl_easy_handle, CURLOPT_HEADER, 0L);
  362. double filesize = 0.0;
  363. if (curl_easy_perform(curl_easy_handle) == CURLE_OK)
  364. {
  365. curl_easy_getinfo(curl_easy_handle, CURLINFO_CONTENT_LENGTH_DOWNLOAD, &filesize);
  366. }
  367. else
  368. {
  369. curl_easy_cleanup(curl_easy_handle);
  370. return -1;
  371. }
  372. curl_easy_cleanup(curl_easy_handle);
  373. return filesize;
  374. }
  375. qint64 CLHFileRemote::curl_Read(char* pBuffer,const char* szRemoteFile,const char* szUser,const char* szPwd,LONGLONG llStart,LONGLONG llSize,long& nProgress)
  376. {
  377. if( pBuffer == NULL)
  378. return -1;
  379. // 将路径转换为url编码格式
  380. QString strPath(szRemoteFile);
  381. //2017.9.30
  382. //strPath = strPath;// gbk->utf8
  383. strPath = UrlEncodeEx(strPath);
  384. if( strPath.left(4).compare("http", Qt::CaseInsensitive) != 0 &&
  385. strPath.left(3).compare("ftp", Qt::CaseInsensitive) !=0 )
  386. {
  387. qint64 nReadTotal = 0;
  388. strPath.replace("/","\\");
  389. LoginServer(strPath.toStdString().c_str(),szUser,szPwd);
  390. try
  391. {
  392. QFile fRemote(strPath);
  393. if( fRemote.open(QIODevice::ReadOnly) == false )
  394. {
  395. m_strError = QString("打开文件失败(%1),%2").arg(GetLastError(), strPath);
  396. return false;
  397. }
  398. qint64 nSize = fRemote.size();
  399. if( llStart>=0 && llSize>0 )
  400. {
  401. nSize = llSize;
  402. }
  403. fRemote.seek(llStart);
  404. int nBufferSize = 76800;
  405. while( nSize>0 )
  406. {
  407. if( nBufferSize > nSize )
  408. nBufferSize = nSize;
  409. int nRead = fRemote.read(pBuffer+nReadTotal,nBufferSize);
  410. if( nRead <= 0 )
  411. {
  412. break;
  413. }
  414. nSize -= nRead;
  415. nReadTotal += nRead;
  416. nProgress = (nReadTotal*100)/llSize;
  417. }
  418. fRemote.close();
  419. }
  420. catch (...)
  421. {
  422. }
  423. return nReadTotal;
  424. }
  425. // 获取curl句柄
  426. CURL *curl_easy_handle = curl_easy_init();
  427. if (NULL == curl_easy_handle)
  428. {
  429. m_strError = "CURL初始化失败";
  430. return -1;
  431. }
  432. if( strPath.left(5).compare("https", Qt::CaseInsensitive) == 0)
  433. {
  434. //curl_easy_setopt(curl_easy_handle, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V6);
  435. curl_easy_setopt(curl_easy_handle, CURLOPT_SSL_VERIFYPEER, FALSE); // 跳过证书检查
  436. curl_easy_setopt(curl_easy_handle, CURLOPT_SSL_VERIFYHOST, FALSE); // 从证书中检查SSL加密算法是否存在
  437. }
  438. // 设置curl_easy_handle属性
  439. if(strlen(szUser)>0)
  440. {
  441. curl_easy_setopt(curl_easy_handle, CURLOPT_USERNAME, szUser);
  442. curl_easy_setopt(curl_easy_handle, CURLOPT_PASSWORD, szPwd);
  443. }
  444. SUpDownData* pData = new SUpDownData;
  445. if( pData == NULL)
  446. {
  447. m_strError = QString("内存不足");
  448. return -1;
  449. }
  450. pData->pBuffer=pBuffer;
  451. pData->llCurrent = 0;
  452. pData->llTotal =llSize;
  453. pData->pFile = NULL;
  454. pData->nProgress = &nProgress;
  455. pData->llBaseSize = 0;
  456. curl_easy_setopt(curl_easy_handle, CURLOPT_URL, strPath.toStdString().c_str());
  457. curl_easy_setopt(curl_easy_handle, CURLOPT_WRITEFUNCTION, &write_data);
  458. curl_easy_setopt(curl_easy_handle, CURLOPT_WRITEDATA, pData);
  459. curl_easy_setopt(curl_easy_handle, CURLOPT_TIMEOUT,3600);//下载超过60分钟则超时
  460. curl_easy_setopt(curl_easy_handle, CURLOPT_FAILONERROR, 1);
  461. if( llStart >= 0 && llSize > 0 )
  462. {
  463. //char szRange[128]{0};
  464. // XXX:linux下好像要使用%lld来替换%I64d
  465. //sprintf( szRange, "%I64d-%I64d", llStart, llStart + llSize - 1);
  466. QString strRange(QString("%1-%2").arg(llStart).arg(llStart + llSize - 1));
  467. curl_easy_setopt(curl_easy_handle, CURLOPT_RANGE, strRange.toStdString().c_str());
  468. }
  469. if( m_btCurlPASV == 1 )
  470. curl_easy_setopt(curl_easy_handle, CURLOPT_FTP_USE_EPSV,0); //EPSV设置为0,就代表启用PASV
  471. else
  472. curl_easy_setopt(curl_easy_handle, CURLOPT_FTP_USE_EPSV,1);
  473. // 执行上传操作
  474. CURLcode res = curl_easy_perform(curl_easy_handle);
  475. if( res != CURLE_OK)
  476. {
  477. if( m_btCurlPASV == 1 )
  478. curl_easy_setopt(curl_easy_handle, CURLOPT_FTP_USE_EPSV,1);
  479. else
  480. curl_easy_setopt(curl_easy_handle, CURLOPT_FTP_USE_EPSV,0); //EPSV设置为0,就代表启用PASV
  481. res = curl_easy_perform(curl_easy_handle);
  482. if( res == CURLE_OK)
  483. {
  484. if(m_btCurlPASV ==1)
  485. m_btCurlPASV = 0;
  486. else
  487. m_btCurlPASV = 1;
  488. }
  489. }
  490. long retcode = 0;
  491. CURLcode code = curl_easy_getinfo(curl_easy_handle, CURLINFO_RESPONSE_CODE, &retcode);
  492. if (res != CURLE_OK)
  493. {
  494. m_strError = QString("源文件下载失败,返回码:%1(%2),%3").arg(res).arg(retcode).arg(curl_easy_strerror(res));
  495. return -1;
  496. }
  497. // 释放资源
  498. curl_easy_cleanup(curl_easy_handle);
  499. int nReturn = pData->llCurrent;
  500. if( pData)
  501. {
  502. delete pData;
  503. pData = NULL;
  504. }
  505. if(CURLE_OK == res && retcode>=200 && retcode<300)
  506. {
  507. }
  508. else
  509. {
  510. m_strError = QString("源文件可能不存在,返回码:%1(%2),%3").arg(code).arg(retcode).arg(curl_easy_strerror(res) );
  511. return -1;
  512. }
  513. return nReturn;
  514. }
  515. bool CLHFileRemote::curl_Download(const char* szLocalFile, const char* szRemoteFile,const char* szUser,const char* szPwd,qint64 llStart,qint64 llSize,bool bAppendFile,qint64 nTotal,qint64 nBaseSize,long& nProgress)
  516. {
  517. // 打开本地文件
  518. QFile fp(szLocalFile);
  519. if( bAppendFile )
  520. {
  521. fp.open(QIODevice::Append);
  522. }
  523. else
  524. {
  525. QFile::remove(szLocalFile);
  526. fp.open(QIODevice::WriteOnly);
  527. }
  528. if (!fp.isOpen())
  529. {
  530. qInfo() << fp.errorString();
  531. m_strError = QString("打开本地文件失败:%1").arg(szLocalFile);
  532. return false;
  533. }
  534. // 获取文件大小
  535. //fseek(fp, 0, SEEK_END);
  536. //qint64 file_size = (qint64)ftell(fp);
  537. //rewind(fp);
  538. // 将路径转换为url编码格式
  539. QString strPath(szRemoteFile);
  540. //2017.9.30
  541. //strPath = strPath;// gbk->utf8
  542. strPath = UrlEncodeEx(strPath);
  543. if( strPath.left(4).compare("http", Qt::CaseInsensitive) != 0 &&
  544. strPath.left(3).compare("ftp", Qt::CaseInsensitive) !=0 )
  545. {
  546. strPath.replace("/","\\");
  547. LoginServer(strPath.toStdString().c_str(),szUser,szPwd);
  548. try
  549. {
  550. QFile fRemote(strPath);
  551. if( fRemote.open(QIODevice::ReadOnly) == false )
  552. {
  553. fp.close();
  554. QFile::remove(szLocalFile);
  555. m_strError = QString("打开文件失败(%1),%2").arg(GetLastError()).arg(strPath);
  556. return false;
  557. }
  558. qint64 nSize = fRemote.size();
  559. if( llStart>=0 && llSize>0 )
  560. {
  561. nSize = llSize;
  562. }
  563. fRemote.seek(llStart);
  564. int nBufferSize = 76800;
  565. char* pBuffer = new char[nBufferSize]{0};
  566. if(pBuffer == NULL)
  567. {
  568. fp.close();
  569. QFile::remove(szLocalFile);
  570. m_strError = QString("内存不足");
  571. return false;
  572. }
  573. qint64 nReadTotal = 0;
  574. memset(pBuffer,0,nBufferSize);
  575. while( nSize>0 )
  576. {
  577. if( nSize < nBufferSize )
  578. nBufferSize = nSize;
  579. int nRead = fRemote.read(pBuffer,nBufferSize);
  580. if( nRead <= 0 )
  581. {
  582. break;
  583. }
  584. fp.write(pBuffer, nRead);
  585. nSize -= nRead;
  586. nReadTotal += nRead;
  587. if( nTotal > 0)
  588. nProgress = (( nBaseSize + nReadTotal)*100)/nTotal;
  589. }
  590. fRemote.close();
  591. fp.close();
  592. delete []pBuffer;
  593. pBuffer = nullptr;
  594. }
  595. catch (...)
  596. {
  597. }
  598. return true;
  599. }
  600. // 获取curl句柄
  601. CURL *curl_easy_handle = curl_easy_init();
  602. if (NULL == curl_easy_handle)
  603. {
  604. fp.close();
  605. QFile::remove(szLocalFile);
  606. m_strError = "CURL初始化失败";
  607. return false;
  608. }
  609. if( strPath.left(5).compare("https", Qt::CaseInsensitive) == 0)
  610. {
  611. //curl_easy_setopt(curl_easy_handle, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V6);
  612. curl_easy_setopt(curl_easy_handle, CURLOPT_SSL_VERIFYPEER, FALSE); // 跳过证书检查
  613. curl_easy_setopt(curl_easy_handle, CURLOPT_SSL_VERIFYHOST, FALSE); // 从证书中检查SSL加密算法是否存在
  614. }
  615. // 设置curl_easy_handle属性
  616. if(strlen(szUser)>0)
  617. {
  618. curl_easy_setopt(curl_easy_handle, CURLOPT_USERNAME, szUser);
  619. curl_easy_setopt(curl_easy_handle, CURLOPT_PASSWORD, szPwd);
  620. }
  621. fp.close();
  622. FILE* ffp = fopen(szLocalFile, "ab+");
  623. if (NULL == ffp) {
  624. return false;
  625. }
  626. SUpDownData* pData = new SUpDownData;
  627. if( pData == NULL)
  628. {
  629. fclose(ffp);
  630. QFile::remove(szLocalFile);
  631. m_strError = QString("内存不足");
  632. return false;
  633. }
  634. pData->pBuffer=NULL;
  635. pData->llCurrent = 0;
  636. pData->llTotal =nTotal;
  637. pData->pFile = ffp;
  638. pData->nProgress = &nProgress;
  639. pData->llBaseSize = nBaseSize;
  640. curl_easy_setopt(curl_easy_handle, CURLOPT_URL, strPath.toStdString().c_str());
  641. curl_easy_setopt(curl_easy_handle, CURLOPT_WRITEFUNCTION, &write_data);
  642. curl_easy_setopt(curl_easy_handle, CURLOPT_WRITEDATA, pData);
  643. curl_easy_setopt(curl_easy_handle, CURLOPT_TIMEOUT,3600);//下载超过60分钟则超时
  644. curl_easy_setopt(curl_easy_handle, CURLOPT_FAILONERROR, 1);
  645. if( llStart>=0 && llSize>0 )
  646. {
  647. //char szRange[128] ={};
  648. //sprintf( szRange, "%I64d-%I64d", llStart,llStart+llSize-1);
  649. QString strRange(QString("%1-%2").arg(llStart).arg(llStart+llSize-1));
  650. curl_easy_setopt(curl_easy_handle, CURLOPT_RANGE, strRange.toStdString().c_str());
  651. }
  652. if( m_btCurlPASV == 1 )
  653. curl_easy_setopt(curl_easy_handle, CURLOPT_FTP_USE_EPSV,0); //EPSV设置为0,就代表启用PASV
  654. else
  655. curl_easy_setopt(curl_easy_handle, CURLOPT_FTP_USE_EPSV,1);
  656. // 执行上传操作
  657. CURLcode res = curl_easy_perform(curl_easy_handle);
  658. if( res != CURLE_OK)
  659. {
  660. if( m_btCurlPASV == 1 )
  661. curl_easy_setopt(curl_easy_handle, CURLOPT_FTP_USE_EPSV,1);
  662. else
  663. curl_easy_setopt(curl_easy_handle, CURLOPT_FTP_USE_EPSV,0); //EPSV设置为0,就代表启用PASV
  664. res = curl_easy_perform(curl_easy_handle);
  665. if( res == CURLE_OK)
  666. {
  667. if(m_btCurlPASV ==1)
  668. m_btCurlPASV = 0;
  669. else
  670. m_btCurlPASV = 1;
  671. }
  672. }
  673. long retcode = 0;
  674. CURLcode code = curl_easy_getinfo(curl_easy_handle, CURLINFO_RESPONSE_CODE, &retcode);
  675. if (res != CURLE_OK)
  676. {
  677. fclose(ffp);
  678. QFile::remove(szLocalFile);
  679. m_strError = QString("源文件下载失败,返回码:%1(%2),%3").arg(res).arg(retcode).arg(curl_easy_strerror(res));
  680. return false;
  681. }
  682. // 释放资源
  683. fclose(ffp);
  684. curl_easy_cleanup(curl_easy_handle);
  685. if( pData)
  686. {
  687. delete pData;
  688. pData = NULL;
  689. }
  690. if(CURLE_OK == res && retcode>=200 && retcode<300)
  691. {
  692. fclose(ffp);//
  693. }
  694. else
  695. {
  696. fclose(ffp);
  697. QFile::remove(szLocalFile);
  698. m_strError = QString("源文件可能不存在,返回码:%1(%2),%3").arg(code).arg(retcode).arg(curl_easy_strerror(res) );
  699. return false;
  700. }
  701. return true;
  702. }