http://blog.csdn.net/zengraoli/article/details/11517367 昨天考慮好了IM傳送圖片的方式,用的是FTP做緩存來(lái)傳遞圖片,,以減少聊天服務(wù)器的壓力,,用的是libcurl庫(kù),自己下載了一個(gè)Server-U 服務(wù)器,,下面是測(cè)試程序,上傳和下載都跑通了,剩下的就是在程序中寫邏輯來(lái)實(shí)現(xiàn)具體的功能了,。
上傳代碼:
- //ftp上傳實(shí)例
- // 服務(wù)器地址:192.168.0.185 賬號(hào):spider 密碼:spider
- // 在服務(wù)器路徑 a上建立一個(gè)a.txt ,本地文件是test.txt
- // 命令行參數(shù)192.168.0.185 spider spider a a.txt D:/test.txt
- //#include <stdlib.h>
- //#include <stdio.h>
- //#include <curl/curl.h>
- //#include <string.h>
- //
- //int debugFun(CURL* curl, curl_infotype type, char* str, size_t len, void* stream)
- //{
- // //只打印CURLINFO_TEXT類型的信息
- // if(type == CURLINFO_TEXT)
- // {
- // fwrite(str, 1, len, (FILE*)stream);
- // }
- // return 0;
- //}
- //
- //int main(int argc, char** argv)
- //{
- // CURL* curl;
- // CURLcode res;
- // char errorBuf[CURL_ERROR_SIZE];
- // FILE *sendFile, *debugFile;
- // char ftpurl[256 + 1];
- // char usrpasswd[64 + 1];
- //
- // curl_slist *slist=NULL;
- //
- // if(argc != 7)
- // {
- // printf("Usage:\n\t./ftp ip username password ftpPath destFileName srcFile");
- // return -1;
- // }
- //
- // //將相關(guān)的調(diào)試信息打印到dubugFile.txt中
- // if(NULL == (debugFile = fopen("debugFile.txt", "a+")))
- // return -1;
- //
- // //打開ftp上傳的源文件
- // if(NULL == (sendFile = fopen(argv[6], "r")))
- // {
- // fclose(debugFile);
- // return -1;
- // }
- //
- // //獲取需要發(fā)送文件的大小
- // fseek(sendFile, 0, SEEK_END);
- // int sendSize = ftell(sendFile);
- // if(sendSize < 0)
- // {
- // fclose(debugFile);
- // fclose(sendFile);
- // return -1;
- // }
- // fseek(sendFile, 0L, SEEK_SET);
- //
- // if((res = curl_global_init(CURL_GLOBAL_ALL)) != 0)
- // {
- // fclose(debugFile);
- // fclose(sendFile);
- // return -1;
- // }
- // if((curl = curl_easy_init()) == NULL)
- // {
- // fclose(debugFile);
- // fclose(sendFile);
- // curl_global_cleanup();
- // return -1;
- // }
- //
- // if(argv[4][strlen(argv[4]) - 1] != '/')
- // sprintf(ftpurl, "ftp://%s/%s/%s", argv[1], argv[4], argv[5]);
- // else
- // sprintf(ftpurl, "ftp://%s/%s%s", argv[1], argv[4], argv[5]);
- // curl_easy_setopt(curl, CURLOPT_URL, ftpurl);
- // //設(shè)置ftp上傳url,組成如下的URL
- // //ftp://192.168.31.145//root/subdir/curl/testftp.txt
- //
- // sprintf(usrpasswd, "%s:%s", argv[2], argv[3]);
- // curl_easy_setopt(curl, CURLOPT_USERPWD, usrpasswd);
- //
- // curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
- // curl_easy_setopt(curl, CURLOPT_DEBUGDATA, debugFile);
- //
- // curl_easy_setopt(curl, CURLOPT_READDATA, sendFile);
- // curl_easy_setopt(curl, CURLOPT_UPLOAD, 1);
- // curl_easy_setopt(curl, CURLOPT_INFILESIZE, sendSize);
- // curl_easy_setopt(curl, CURLOPT_FTP_CREATE_MISSING_DIRS, 1);
- //
- // curl_easy_setopt(curl, CURLOPT_DEBUGFUNCTION, debugFun);
- //
- // res = curl_easy_perform(curl);
- // if(0 != res)
- // {
- // fclose(sendFile);
- // fclose(debugFile);
- // curl_easy_cleanup(curl);
- // curl_global_cleanup();
- // return -1;
- // }
- //
- // curl_easy_cleanup(curl);
- // fclose(sendFile);
- // fclose(debugFile);
- // curl_global_cleanup();
- // getchar();
- // return 0;
- //}
下載代碼如下:
- //ftp下載實(shí)例
- #include <stdio.h>;
- #include <curl/curl.h>;
- #include <curl/types.h>;
- #include <curl/easy.h>;
-
- struct FtpFile //定義一個(gè)結(jié)構(gòu)為了傳遞給my_fwrite函數(shù).可用curl_easy_setopt的CURLOPT_WRITEDATA選項(xiàng)傳遞
- {
- char *filename;
- FILE *stream;
- };
-
- int my_fwrite(void *buffer, size_t size, size_t nmemb, void *stream)
- {
- struct FtpFile *out=(struct FtpFile *)stream; // stream指針其實(shí)就是指向struct FtpFile ftpfile的
- if(out && !out->stream)
- {
- out->stream=fopen(out->filename, "wb"); //沒有這個(gè)流的話就創(chuàng)建一個(gè)名字是out->filename.
- if(!out->stream)
- return -1;
- }
- return fwrite(buffer, size, nmemb, out->stream);
- }
-
- int main(int argc, char *argv[])
- {
- CURL *curl;
- CURLcode res;
- struct FtpFile ftpfile={"D:/Success.txt",NULL}; //初始化一個(gè)FtpFile結(jié)構(gòu)
- curl_global_init(CURL_GLOBAL_DEFAULT);
-
- curl = curl_easy_init();
- if(curl)
- {
- curl_easy_setopt(curl, CURLOPT_URL,"ftp://192.168.0.185/a/a.txt");
-
- curl_easy_setopt(curl, CURLOPT_USERPWD,"spider:spider");
- curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, my_fwrite);
- curl_easy_setopt(curl, CURLOPT_WRITEDATA, &ftpfile); //給相關(guān)函數(shù)的第四個(gè)參數(shù)傳遞一個(gè)結(jié)構(gòu)體的指針
- curl_easy_setopt(curl, CURLOPT_VERBOSE, TRUE);//CURLOPT_VERBOSE 這個(gè)選項(xiàng)很常用用來(lái)在屏幕上顯示對(duì)服務(wù)器相關(guān)操作返回的信息
-
- res = curl_easy_perform(curl);
- curl_easy_cleanup(curl);
-
- if(CURLE_OK != res)
- fprintf(stderr, "curl told us %d\n", res);
- }
- if(ftpfile.stream)
- fclose(ftpfile.stream);
- curl_global_cleanup();
-
- return 0;
- }
最后記錄一個(gè)比較好用的:
- #include <stdlib.h>
- #include <stdio.h>
- #include <curl/curl.h>
- #include <sys/stat.h>
- /* parse headers for Content-Length */
- size_t getcontentlengthfunc(void *ptr, size_t size, size_t nmemb, void *stream)
- {
- int r;
- long len = 0;
- /* _snscanf() is Win32 specific */
- //r = _snscanf(ptr, size * nmemb, "Content-Length: %ld\n", &len);
- r = sscanf((const char*)ptr, "Content-Length: %ld\n", &len);
- if (r) /* Microsoft: we don't read the specs */
- *((long *) stream) = len;
- return size * nmemb;
- }
- /* discard downloaded data */
- size_t discardfunc(void *ptr, size_t size, size_t nmemb, void *stream)
- {
- return size * nmemb;
- }
- //write data to upload
- size_t writefunc(void *ptr, size_t size, size_t nmemb, void *stream)
- {
- return fwrite(ptr, size, nmemb, (FILE*)stream);
- }
- /* read data to upload */
- size_t readfunc(void *ptr, size_t size, size_t nmemb, void *stream)
- {
- FILE *f = (FILE*)stream;
- size_t n;
- if (ferror(f))
- return CURL_READFUNC_ABORT;
- n = fread(ptr, size, nmemb, f) * size;
- return n;
- }
- int upload(CURL *curlhandle, const char * remotepath, const char * localpath, long timeout, long tries)
- {
- FILE *f;
- long uploaded_len = 0;
- CURLcode r = CURLE_GOT_NOTHING;
- int c;
- f = fopen(localpath, "rb");
- if (f == NULL) {
- perror(NULL);
- return 0;
- }
- curl_easy_setopt(curlhandle, CURLOPT_UPLOAD, 1L);
- curl_easy_setopt(curlhandle, CURLOPT_URL, remotepath);
- curl_easy_setopt(curlhandle, CURLOPT_USERPWD, "spider:spider");
- if (timeout)
- curl_easy_setopt(curlhandle, CURLOPT_FTP_RESPONSE_TIMEOUT, timeout);
- curl_easy_setopt(curlhandle, CURLOPT_HEADERFUNCTION, getcontentlengthfunc);
- curl_easy_setopt(curlhandle, CURLOPT_HEADERDATA, &uploaded_len);
- curl_easy_setopt(curlhandle, CURLOPT_WRITEFUNCTION, discardfunc);
- curl_easy_setopt(curlhandle, CURLOPT_READFUNCTION, readfunc);
- curl_easy_setopt(curlhandle, CURLOPT_READDATA, f);
- curl_easy_setopt(curlhandle, CURLOPT_FTPPORT, "-"); /* disable passive mode */
- curl_easy_setopt(curlhandle, CURLOPT_FTP_CREATE_MISSING_DIRS, 1L);
- curl_easy_setopt(curlhandle, CURLOPT_VERBOSE, 1L);
- for (c = 0; (r != CURLE_OK) && (c < tries); c++) {
- /* are we resuming? */
- if (c) { /* yes */
- /* determine the length of the file already written */
- /*
- * With NOBODY and NOHEADER, libcurl will issue a SIZE
- * command, but the only way to retrieve the result is
- * to parse the returned Content-Length header. Thus,
- * getcontentlengthfunc(). We need discardfunc() above
- * because HEADER will dump the headers to stdout
- * without it.
- */
- curl_easy_setopt(curlhandle, CURLOPT_NOBODY, 1L);
- curl_easy_setopt(curlhandle, CURLOPT_HEADER, 1L);
- r = curl_easy_perform(curlhandle);
- if (r != CURLE_OK)
- continue;
- curl_easy_setopt(curlhandle, CURLOPT_NOBODY, 0L);
- curl_easy_setopt(curlhandle, CURLOPT_HEADER, 0L);
- fseek(f, uploaded_len, SEEK_SET);
- curl_easy_setopt(curlhandle, CURLOPT_APPEND, 1L);
- }
- else { /* no */
- curl_easy_setopt(curlhandle, CURLOPT_APPEND, 0L);
- }
- r = curl_easy_perform(curlhandle);
- }
- fclose(f);
- if (r == CURLE_OK)
- return 1;
- else {
- fprintf(stderr, "%s\n", curl_easy_strerror(r));
- return 0;
- }
- }
- // 下載
- int download(CURL *curlhandle, const char * remotepath, const char * localpath, long timeout, long tries)
- {
- FILE *f;
- curl_off_t local_file_len = -1 ;
- long filesize =0 ;
- CURLcode r = CURLE_GOT_NOTHING;
- struct stat file_info;
- int use_resume = 0;
- //獲取本地文件大小信息
- if(stat(localpath, &file_info) == 0)
- {
- local_file_len = file_info.st_size;
- use_resume = 1;
- }
- //追加方式打開文件,,實(shí)現(xiàn)斷點(diǎn)續(xù)傳
- f = fopen(localpath, "ab+");
- if (f == NULL) {
- perror(NULL);
- return 0;
- }
- curl_easy_setopt(curlhandle, CURLOPT_URL, remotepath);
- curl_easy_setopt(curlhandle, CURLOPT_USERPWD, "spider:spider");
- //連接超時(shí)設(shè)置
- curl_easy_setopt(curlhandle, CURLOPT_CONNECTTIMEOUT, timeout);
- //設(shè)置頭處理函數(shù)
- curl_easy_setopt(curlhandle, CURLOPT_HEADERFUNCTION, getcontentlengthfunc);
- curl_easy_setopt(curlhandle, CURLOPT_HEADERDATA, &filesize);
- // 設(shè)置斷點(diǎn)續(xù)傳
- curl_easy_setopt(curlhandle, CURLOPT_RESUME_FROM_LARGE, use_resume?local_file_len:0);
- curl_easy_setopt(curlhandle, CURLOPT_WRITEFUNCTION, writefunc);
- curl_easy_setopt(curlhandle, CURLOPT_WRITEDATA, f);
- curl_easy_setopt(curlhandle, CURLOPT_NOPROGRESS, 1L);
- curl_easy_setopt(curlhandle, CURLOPT_VERBOSE, 1L);
- r = curl_easy_perform(curlhandle);
- fclose(f);
- if (r == CURLE_OK)
- return 1;
- else {
- fprintf(stderr, "%s\n", curl_easy_strerror(r));
- return 0;
- }
- }
- int main(int c, char **argv)
- {
- CURL *curlhandle = NULL;
- CURL *curldwn = NULL;
- curl_global_init(CURL_GLOBAL_ALL);
- curlhandle = curl_easy_init();
- curldwn = curl_easy_init();
- upload(curlhandle, "ftp://192.168.0.185/a/success", "D:/abc.jpg", 1, 3);
- download(curldwn, "ftp://192.168.0.185/a/success", "D:/abc1.jpg", 1, 3);
- curl_easy_cleanup(curlhandle);
- curl_easy_cleanup(curldwn);
- curl_global_cleanup();
- return 0;
- }
|