curl tftp libcurl 功能使用

古城微笑少年丶 2022-03-16 09:36 354阅读 0赞
  1. #include <curl/curl.h>
  2. static size_t read_callback(void *ptr, size_t size, size_t nmemb, void *stream)
  3. {
  4. curl_off_t nread;
  5. /* in real-world cases, this would probably get this data differently
  6. as this fread() stuff is exactly what the library already would do
  7. by default internally */
  8. size_t retcode = fread(ptr, size, nmemb, (FILE*)stream);
  9. nread = (curl_off_t)retcode;
  10. fprintf(stderr, "*** We read %" CURL_FORMAT_CURL_OFF_T
  11. " bytes from file\n", nread);
  12. return retcode;
  13. }
  14. //tftp协议上传
  15. void fileUploadTftp()
  16. {
  17. CURL *curl;
  18. CURLcode res;
  19. FILE *hd_src;
  20. struct stat file_info;
  21. curl_off_t fsize;
  22. QString locale = ui->lineEdit_path->text();
  23. QFileInfo fileInfo(locale);
  24. QString fileName = fileInfo.fileName();
  25. char *locale_file = locale.toLatin1().data(); //本地文件名
  26. QString remote = "tftp://ip:69/" + fileName;
  27. char *remote_url = remote.toLatin1().data(); //服务器路径
  28. struct curl_slist *headerlist;
  29. /* get the file size of the local file */
  30. if(stat(locale_file, &file_info)) {
  31. printf("Couldnt open '%s': %s\n", locale_file, strerror(errno));
  32. return;
  33. }
  34. fsize = (curl_off_t)file_info.st_size;
  35. printf("Local file size: %" CURL_FORMAT_CURL_OFF_T " bytes.\n", fsize);
  36. /* get a FILE * of the same file */
  37. hd_src = fopen(locale_file, "rb");
  38. /* In windows, this will init the winsock stuff */
  39. curl_global_init(CURL_GLOBAL_ALL);
  40. /* get a curl handle */
  41. curl = curl_easy_init();
  42. if(curl)
  43. {
  44. /* we want to use our own read function */
  45. curl_easy_setopt(curl, CURLOPT_READFUNCTION, read_callback);
  46. /* enable uploading */
  47. curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);
  48. /* specify target */
  49. curl_easy_setopt(curl,CURLOPT_URL, remote_url);
  50. /* pass in that last of FTP commands to run after the transfer */
  51. curl_easy_setopt(curl, CURLOPT_POSTQUOTE, headerlist);
  52. /* now specify which file to upload */
  53. curl_easy_setopt(curl, CURLOPT_READDATA, hd_src);
  54. /* Set the size of the file to upload (optional). If you give a *_LARGE
  55. option you MUST make sure that the type of the passed-in argument is a
  56. curl_off_t. If you use CURLOPT_INFILESIZE (without _LARGE) you must
  57. make sure that to pass in a type 'long' argument. */
  58. curl_easy_setopt(curl, CURLOPT_INFILESIZE_LARGE,
  59. (curl_off_t)fsize);
  60. /* Now run off and do what you've been told! */
  61. res = curl_easy_perform(curl);
  62. /* Check for errors */
  63. if(res != CURLE_OK)
  64. fprintf(stderr, "curl_easy_perform() failed: %s\n",
  65. curl_easy_strerror(res));
  66. /* clean up the FTP commands list */
  67. curl_slist_free_all (headerlist);
  68. /* always cleanup */
  69. curl_easy_cleanup(curl);
  70. }
  71. fclose(hd_src); /* close the local file */
  72. curl_global_cleanup();
  73. }

发表评论

表情:
评论列表 (有 0 条评论,354人围观)

还没有评论,来说两句吧...

相关阅读