linux下c語言使用libcurl和實現檔案上傳__oracle

來源:互聯網
上載者:User
#include <stdio.h>#include <sys/io.h> #include <stdlib.h>#include <errno.h>#include <string.h>#include <fcntl.h>#include <sys/types.h>#include <sys/socket.h>#include <time.h> #include <curl/curl.h>#include <sys/stat.h> #include <dirent.h>#include <sys/inotify.h>  static char g_last_name[20]="upload_audio.mp3";int sendPostFile();unsigned long get_file_size();unsigned long get_file_size()  {      unsigned long filesize = -1;          struct stat statbuff;      if(stat("/home/user/upload_audio.mp3", &statbuff) < 0){          return filesize;      }else{          filesize = statbuff.st_size;      }      return filesize;  }  int sendPostFile(){  CURL *curl;  CURLcode res;  char filesize[15]={0};  sprintf(filesize, "%lu", get_file_size);  struct curl_httppost *formpost=NULL;  struct curl_httppost *lastptr=NULL;  struct curl_slist *headerlist=NULL;  static const char buf[] = "Expect:";     curl_global_init(CURL_GLOBAL_ALL);      /* Fill in the file upload field */       curl_formadd(&formpost,               &lastptr,               CURLFORM_COPYNAME, "uploadfile",               CURLFORM_FILE, g_name,                  CURLFORM_CONTENTTYPE, "application/octet-stream",                CURLFORM_END);        printf("curl_formadd filesize\n");     curl_formadd(&formpost,               &lastptr,               CURLFORM_COPYNAME, "filesize",               CURLFORM_COPYCONTENTS, filesize,               CURLFORM_END);      printf("curl_formadd submit\n");     curl_formadd(&formpost,               &lastptr,               CURLFORM_COPYNAME, "upfile",               CURLFORM_COPYCONTENTS, "commit",               CURLFORM_END);       UPLOG("curl curl_easy_init\n");   curl = curl_easy_init();  /* initalize custom header list (stating that Expect: 100-continue is not     wanted */   headerlist = curl_slist_append(headerlist, buf);  if(curl) {UPLOG("curl true begin post\n");    /* what URL that receives this POST */     curl_easy_setopt(curl, CURLOPT_URL, POST_FILE_URL);    curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headerlist);    curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost);    //curl_easy_setopt(curl, CURLOPT_INFILESIZE_LARGE, 1613);     /* Perform the request, res will get the return code */     res = curl_easy_perform(curl);    /* Check for errors */     if(res != CURLE_OK){      fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res));  UPLOG("curl url_easy_perform() failed:%s \n",curl_easy_strerror(res));  return -1;}     /* always cleanup */     curl_easy_cleanup(curl);     /* then cleanup the formpost chain */     curl_formfree(formpost);    /* free slist */     curl_slist_free_all (headerlist);  }  return 0;}void main(int argc, char *argv[]){      if(0==sendPostFile()){printf("sendPostFile success\n"); }else{printf("sendPostFile failed\n"); }}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.