网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。
一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!
***************************************************************************/
#include <stdio.h>
#include <curl/curl.h>
/*
- Similar to ftpget.c but also stores the received response-lines
- in a separate file using our own callback!
*/
static size_t
write_response(void *ptr, size_t size, size_t nmemb, void *data)
{
FILE *writehere = (FILE *)data;
return fwrite(ptr, size, nmemb, writehere);
}
#define FTPBODY “ftp-list”
#define FTPHEADERS “ftp-responses”
int main(void)
{
CURL *curl;
CURLcode res;
FILE *ftpfile;
FILE *respfile;
/* local file name to store the file as /
ftpfile = fopen(FTPBODY, “wb”); / b is binary, needed on win32 */
/* local file name to store the FTP server’s response lines in /
respfile = fopen(FTPHEADERS, “wb”); / b is binary, needed on win32 */
curl = curl_easy_init();
if(curl) {
/* Get a file listing from sunet /
curl_easy_setopt(curl, CURLOPT_URL, “http://10.170.6.66/testfile”);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, ftpfile);
/ If you intend to use this on windows with a libcurl DLL, you must use
CURLOPT_WRITEFUNCTION as well /
curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, write_response);
curl_easy_setopt(curl, CURLOPT_HEADERDATA, respfile);
res = curl_easy_perform(curl);
/ Check for errors */
if(res != CURLE_OK)
fprintf(stderr, “curl_easy_perform() failed: %s\n”,
curl_easy_strerror(res));
/* always cleanup */
curl_easy_cleanup(curl);
}
fclose(ftpfile); /* close the local file /
fclose(respfile); / close the response file */
return 0;
}
## Get a single file from an FTPS server.2
/***************************************************************************
-
_ _ ____ _
- Project ___| | | | _ | |
-
/ __| | | | |_) | |
-
| (__| |_| | _ <| |___
-
\___|\___/|_| \_\_____|
- Copyright © 1998 - 2019, Daniel Stenberg, daniel@haxx.se, et al.
- This software is licensed as described in the file COPYING, which
- you should have received as part of this distribution. The terms
- are also available at https://curl.haxx.se/docs/copyright.html.
- You may opt to use, copy, modify, merge, publish, distribute and/or sell
- copies of the Software, and permit persons to whom the Software is
- furnished to do so, under the terms of the COPYING file.
- This software is distributed on an “AS IS” basis, WITHOUT WARRANTY OF ANY
- KIND, either express or implied.
***************************************************************************/
#include <stdio.h>
#include <curl/curl.h>
/*
- Get a single file from an FTPS server.
*/
struct FtpFile {
const char *filename;
FILE *stream;
};
static size_t my_fwrite(void *buffer, size_t size, size_t nmemb,
void *stream)
{
struct FtpFile *out = (struct FtpFile )stream;
if(!out->stream) {
/ open file for writing /
out->stream = fopen(out->filename, “wb”);
if(!out->stream)
return -1; / failure, can’t open file to write */
}
return fwrite(buffer, size, nmemb, out->stream);
}
int main(void)
{
CURL curl;
CURLcode res;
struct FtpFile ftpfile = {
“yourfile.txt”, / name to store the file as if successful */
NULL
};
curl_global_init(CURL_GLOBAL_DEFAULT);
curl = curl_easy_init();
if(curl) {
/*
* You better replace the URL with one that works! Note that we use an
* FTP:// URL with standard explicit FTPS. You can also do FTPS:// URLs if
* you want to do the rarer kind of transfers: implicit.
/
curl_easy_setopt(curl, CURLOPT_URL,
“http://10.170.6.66/testfile”);
/ Define our callback to get called when there’s data to be written /
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, my_fwrite);
/ Set a pointer to our struct to pass to the callback */
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &ftpfile);
/* We activate SSL and we require it for both control and data */
curl_easy_setopt(curl, CURLOPT_USE_SSL, CURLUSESSL_ALL);
/* Switch on full protocol/debug output */
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
res = curl_easy_perform(curl);
/* always cleanup */
curl_easy_cleanup(curl);
if(CURLE_OK != res) {
/* we failed */
fprintf(stderr, "curl told us %d\n", res);
}
}
if(ftpfile.stream)
fclose(ftpfile.stream); /* close the local file */
curl_global_cleanup();
return 0;
}
## Performs an FTP upload and renames the file just after a successful transfer.
/***************************************************************************
-
_ _ ____ _
- Project ___| | | | _ | |
-
/ __| | | | |_) | |
-
| (__| |_| | _ <| |___
-
\___|\___/|_| \_\_____|
- Copyright © 1998 - 2019, Daniel Stenberg, daniel@haxx.se, et al.
- This software is licensed as described in the file COPYING, which
- you should have received as part of this distribution. The terms
- are also available at https://curl.haxx.se/docs/copyright.html.
- You may opt to use, copy, modify, merge, publish, distribute and/or sell
- copies of the Software, and permit persons to whom the Software is
- furnished to do so, under the terms of the COPYING file.
- This software is distributed on an “AS IS” basis, WITHOUT WARRANTY OF ANY
- KIND, either express or implied.
***************************************************************************/
#include <stdio.h>
#include <string.h>
#include <curl/curl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#ifdef WIN32
#include <io.h>
#else
#include <unistd.h>
#endif
/*
- Performs an FTP upload and renames the file just after a successful
- transfer.
*/
//#define LOCAL_FILE “/tmp/uploadthis.txt”
//#define UPLOAD_FILE_AS “while-uploading.txt”
//#define REMOTE_URL “ftp://example.com/” UPLOAD_FILE_AS
//#define RENAME_FILE_TO “renamed-and-fine.txt”
#define LOCAL_FILE “uploadthis.txt”
#define UPLOAD_FILE_AS “while-uploading.txt”
#define REMOTE_URL “http://10.170.6.66/”
#define RENAME_FILE_TO “renamed-and-fine.txt”
/* NOTE: if you want this example to work on Windows with libcurl as a
DLL, you MUST also provide a read callback with CURLOPT_READFUNCTION.
Failing to do so will give you a crash since a DLL may not use the
variable’s memory when passed in to it from an app like this. */
static size_t read_callback(void *ptr, size_t size, size_t nmemb, void stream)
{
curl_off_t nread;
/ in real-world cases, this would probably get this data differently
as this fread() stuff is exactly what the library already would do
by default internally */
size_t retcode = fread(ptr, size, nmemb, stream);
nread = (curl_off_t)retcode;
fprintf(stderr, “*** We read %” CURL_FORMAT_CURL_OFF_T
" bytes from file\n", nread);
return retcode;
}
int main(void)
{
CURL *curl;
CURLcode res;
FILE *hd_src;
struct stat file_info;
curl_off_t fsize;
struct curl_slist *headerlist = NULL;
static const char buf_1 [] = "RNFR " UPLOAD_FILE_AS;
static const char buf_2 [] = "RNTO " RENAME_FILE_TO;
/* get the file size of the local file */
if(stat(LOCAL_FILE, &file_info)) {
printf(“Couldn’t open ‘%s’: %s\n”, LOCAL_FILE, strerror(errno));
return 1;
}
fsize = (curl_off_t)file_info.st_size;
printf(“Local file size: %” CURL_FORMAT_CURL_OFF_T " bytes.\n", fsize);
/* get a FILE * of the same file */
hd_src = fopen(LOCAL_FILE, “rb”);
/* In windows, this will init the winsock stuff */
curl_global_init(CURL_GLOBAL_ALL);
/* get a curl handle /
curl = curl_easy_init();
if(curl) {
/ build a list of commands to pass to libcurl */
headerlist = curl_slist_append(headerlist, buf_1);
headerlist = curl_slist_append(headerlist, buf_2);
/* we want to use our own read function */
curl_easy_setopt(curl, CURLOPT_READFUNCTION, read_callback);
/* enable uploading */
curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);
/* specify target */
curl_easy_setopt(curl, CURLOPT_URL, REMOTE_URL);
/* pass in that last of FTP commands to run after the transfer */
curl_easy_setopt(curl, CURLOPT_POSTQUOTE, headerlist);
/* now specify which file to upload */
curl_easy_setopt(curl, CURLOPT_READDATA, hd_src);
/* Set the size of the file to upload (optional). If you give a *_LARGE
option you MUST make sure that the type of the passed-in argument is a
curl_off_t. If you use CURLOPT_INFILESIZE (without _LARGE) you must
make sure that to pass in a type 'long' argument. */
curl_easy_setopt(curl, CURLOPT_INFILESIZE_LARGE,
(curl_off_t)fsize);
/* Now run off and do what you've been told! */
res = curl_easy_perform(curl);
/* Check for errors */
if(res != CURLE_OK)
fprintf(stderr, "curl_easy_perform() failed: %s\n",
curl_easy_strerror(res));
/* clean up the FTP commands list */
curl_slist_free_all(headerlist);
/* always cleanup */
curl_easy_cleanup(curl);
}
fclose(hd_src); /* close the local file */
curl_global_cleanup();
return 0;
}
## FTP upload a file from memory
/***************************************************************************
-
_ _ ____ _
- Project ___| | | | _ | |
-
/ __| | | | |_) | |
-
| (__| |_| | _ <| |___
-
\___|\___/|_| \_\_____|
- Copyright © 1998 - 2017, Daniel Stenberg, daniel@haxx.se, et al.
- This software is licensed as described in the file COPYING, which
- you should have received as part of this distribution. The terms
- are also available at https://curl.haxx.se/docs/copyright.html.
- You may opt to use, copy, modify, merge, publish, distribute and/or sell
- copies of the Software, and permit persons to whom the Software is
- furnished to do so, under the terms of the COPYING file.
- This software is distributed on an “AS IS” basis, WITHOUT WARRANTY OF ANY
- KIND, either express or implied.
**************************************************************************/
/
- FTP upload a file from memory
*/
#include <stdio.h>
#include <string.h>
#include <curl/curl.h>
static const char data[]=
"Lorem ipsum dolor sit amet, consectetur adipiscing elit. "
"Nam rhoncus odio id venenatis volutpat. Vestibulum dapibus "
"bibendum ullamcorper. Maecenas finibus elit augue, vel "
"condimentum odio maximus nec. In hac habitasse platea dictumst. "
"Vestibulum vel dolor et turpis rutrum finibus ac at nulla. "
"Vivamus nec neque ac elit blandit pretium vitae maximus ipsum. "
"Quisque sodales magna vel erat auctor, sed pellentesque nisi "
"rhoncus. Donec vehicula maximus pretium. Aliquam eu tincidunt "
“lorem.”;
struct WriteThis {
const char *readptr;
size_t sizeleft;
};
static size_t read_callback(void *ptr, size_t size, size_t nmemb, void *userp)
{
struct WriteThis *upload = (struct WriteThis )userp;
size_t max = sizenmemb;
if(max < 1)
return 0;
if(upload->sizeleft) {
size_t copylen = max;
if(copylen > upload->sizeleft)
copylen = upload->sizeleft;
memcpy(ptr, upload->readptr, copylen);
upload->readptr += copylen;
upload->sizeleft -= copylen;
return copylen;
}
return 0; /* no more data left to deliver */
}
int main(void)
{
CURL *curl;
CURLcode res;
struct WriteThis upload;
upload.readptr = data;
upload.sizeleft = strlen(data);
/* In windows, this will init the winsock stuff /
res = curl_global_init(CURL_GLOBAL_DEFAULT);
/ Check for errors */
if(res != CURLE_OK) {
fprintf(stderr, “curl_global_init() failed: %s\n”,
curl_easy_strerror(res));
return 1;
}
/* get a curl handle /
curl = curl_easy_init();
if(curl) {
/ First set the URL, the target file */
curl_easy_setopt(curl, CURLOPT_URL,
“ftp://example.com/path/to/upload/file”);
/* User and password for the FTP login */
curl_easy_setopt(curl, CURLOPT_USERPWD, "login:secret");
/* Now specify we want to UPLOAD data */
curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);
/* we want to use our own read function */
curl_easy_setopt(curl, CURLOPT_READFUNCTION, read_callback);
/* pointer to pass to our read function */
curl_easy_setopt(curl, CURLOPT_READDATA, &upload);
/* get verbose debug output please */
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
/* Set the expected upload size. */
curl_easy_setopt(curl, CURLOPT_INFILESIZE_LARGE,
(curl_off_t)upload.sizeleft);
/* 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));
/* always cleanup */
curl_easy_cleanup(curl);
}
curl_global_cleanup();
return 0;
}
## Upload to FTP, resuming failed transfers.
/***************************************************************************
-
_ _ ____ _
- Project ___| | | | _ | |
-
/ __| | | | |_) | |
-
| (__| |_| | _ <| |___
-
\___|\___/|_| \_\_____|
- Copyright © 1998 - 2017, Daniel Stenberg, daniel@haxx.se, et al.
- This software is licensed as described in the file COPYING, which
- you should have received as part of this distribution. The terms
- are also available at https://curl.haxx.se/docs/copyright.html.
- You may opt to use, copy, modify, merge, publish, distribute and/or sell
- copies of the Software, and permit persons to whom the Software is
- furnished to do so, under the terms of the COPYING file.
- This software is distributed on an “AS IS” basis, WITHOUT WARRANTY OF ANY
- KIND, either express or implied.
**************************************************************************/
/
- Upload to FTP, resuming failed transfers.
*/
#include <stdlib.h>
#include <stdio.h>
#include <curl/curl.h>
/* parse headers for Content-Length */
static size_t getcontentlengthfunc(void *ptr, size_t size, size_t nmemb,
void *stream)
{
int r;
long len = 0;
r = sscanf(ptr, “Content-Length: %ld\n”, &len);
if®
*((long *) stream) = len;
return size * nmemb;
}
/* discard downloaded data */
static size_t discardfunc(void *ptr, size_t size, size_t nmemb, void *stream)
{
(void)ptr;
(void)stream;
return size * nmemb;
}
/* read data to upload */
static size_t readfunc(void *ptr, size_t size, size_t nmemb, void *stream)
{
FILE *f = stream;
size_t n;
if(ferror(f))
return CURL_READFUNC_ABORT;
n = fread(ptr, size, nmemb, f) * size;
return n;
}
static int upload(CURL *curlhandle, const char *remotepath,
const char *localpath, long timeout, long tries)
{
FILE *f;
网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。
一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!
C_ABORT;
n = fread(ptr, size, nmemb, f) * size;
return n;
}
static int upload(CURL *curlhandle, const char *remotepath,
const char *localpath, long timeout, long tries)
{
FILE *f;
[外链图片转存中…(img-Ohh8Iyk8-1715548665717)]
[外链图片转存中…(img-cXUk6Up3-1715548665718)]
网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。
一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!