Cocos2.2.3@H_404_1@资源在线下载
@H_404_1@
服务器端:
1.下载hfs@H_404_1@服务器,解压缩即可用
2.将资源拖入虚拟文件系统
3.打开cmd@H_404_1@,输入ipconfig@H_404_1@查看IP@H_404_1@地址:192.168.100.11
若没有对应的IP@H_404_1@,则IP@H_404_1@地址——自定义,输入IP@H_404_1@地址,然后再进行选择
Cocos@H_404_1@端:
1.包含#include<curl/curl.h>
2.贴入如下代码:
static size_t downLoadPackage(void *ptr,size_t size,size_t nmemb,void *userdata) {//该函数用于将服务器上的资源写入文件 FILE *fp = (FILE*)userdata; size_t written = fwrite(ptr,size,nmemb,fp); return written; } int DLManagerProgressFunc(void *ptr,double totalToDownload,double nowDownloaded,double totalToUpLoad,double nowUpLoaded) {//用于输出下载进度 CCLog("downloading... %d%%",(int)(nowDownloaded/totalToDownload*100)); return 0; } void HelloWorld::menuCloseCallback(CCObject* pSender) { //资源在服务器上的路径 std::string _packageUrl = "http://192.168.100.11/AssociativeVideo/B.mp4"; //资源存储在本地的路径 std::string _storagePath = CCFileUtils::sharedFileUtils()->getWritablePath() + "B.mp4"; FILE *fp = fopen(_storagePath.c_str(),"wb"); if (! fp) { CCLog("can not create file %s",_storagePath.c_str()); return ; } //通过CURL进行下载 CURL *_curl; _curl = curl_easy_init(); if (! _curl) { CCLog("can not init curl"); return ; } // Download pacakge CURLcode res; curl_easy_setopt(_curl,CURLOPT_URL,_packageUrl.c_str()); curl_easy_setopt(_curl,CURLOPT_WRITEFUNCTION,downLoadPackage); curl_easy_setopt(_curl,CURLOPT_WRITEDATA,fp); curl_easy_setopt(_curl,CURLOPT_NOPROGRESS,false); curl_easy_setopt(_curl,CURLOPT_PROGRESSFUNCTION,DLManagerProgressFunc); curl_easy_setopt(_curl,CURLOPT_PROGRESSDATA,this); res = curl_easy_perform(_curl); curl_easy_cleanup(_curl); if (res != 0) { CCLog("error when download package"); fclose(fp); return ; } //下载成功后输出该语句 CCLog("succeed downloading package %s",_packageUrl.c_str()); fclose(fp); }
原文链接:https://www.f2er.com/cocos2dx/340161.html