FTP를 사용하여 파일을 다운로드하려고하는데 연결이 끝나면 파일을 중단 한 위치에서 다시 시작해야합니다. 내 문제는 연결을 닫고 다시 연결하면 다음 코드 조각을 사용하여 다운로드를 계속할 수 있지만 서버 사이트에서 다운로드를 다시 시작할 수 없으면 프로그램이 무한 상태가됩니다.C에서 Curl을 사용하여 FTP를 통해 파일 다운로드를 재개하는 방법은 무엇입니까?
#include <stdio.h>
#include <curl/curl.h>
/*
* This is an example showing how to get a single file from an FTP server.
* It delays the actual destination file creation until the first write
* callback so that it won't create an empty file in case the remote file
* doesn't exist or something else fails.
*/
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 && !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={
"dev.zip", /* name to store the file as if succesful */
NULL
};
curl_global_init(CURL_GLOBAL_DEFAULT);
curl = curl_easy_init();
if(curl) {
/*
* You better replace the URL with one that works!
*/
curl_easy_setopt(curl, CURLOPT_URL,
"ftp://root:[email protected]/dev.zip");
/* 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);
/* 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;
}
원격 사이트가 연결을 끊으면 어떻게 다운로드를 재개 할 수 있습니까? 어떤 도움 appeand과의 끝에서 다운로드를 재개 할 libcurl에 말할 필요성을 인식하여 쓰기 기능에 FTPFILE 구조체에 가변적이고 추가 Yuvi
ftp에 http의 'AddRange'와 같은 것이 있습니까? –
네,하지만 거기에 내 문제는 연결을 닫으면 오류를 잡을 수 없다는 것입니다. 더 많은 정보를 확인하려면 여기를 클릭하십시오 http://curl.haxx.se/libcurl/c/ftpuploadresume.html – Yuvi